首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel忽略重复查询请求

Laravel忽略重复查询请求
EN

Stack Overflow用户
提问于 2022-10-28 07:45:34
回答 1查看 38关注 0票数 0

所以在我的laravel应用程序中,我为“file”字段做了一个过滤器。

我为不同的角色(管理人员、合作伙伴)做了这个过滤器,首先我让它在合作伙伴端工作(这显示了该角色(不是所有文件)所有文件的可访问性,这是100%的工作。但是现在我想让它在管理中工作(访问所有文件),但是这不起作用,因为它总是返回这个错误:

代码语言:javascript
运行
复制
SQLSTATE[HY093]: Invalid parameter number
SQL:
select * from `file` where `id` in (9, 11, 9, 11, 9, 11, 9, 11, 9, 11)

如您所见,有重复的条目。是否有一种方法可以忽略此错误而只在文件中加载。如果我将查询复制到SQL本身中,它就会正常工作。

管理过滤器的代码:

代码语言:javascript
运行
复制
    if ($tag_id != 0 && $langs != 0) {
            for ($i = 0; $i < $count; $i++) {
                $fileCount = File_Tag::where('tag_id', $tag_id)->pluck('file_id')->count();
                $file[] = $fileCount > 0 ? File_Tag::where('tag_id', $tag_id)->pluck('file_id') : null;
            }
            $this->AdminFiles = File::whereIn('id', $file)->where('language_id', $langs)->get();
        } else if ($tag_id != 0) {
            for ($i = 0; $i < $count; $i++) {
                $fileCount = File_Tag::where('tag_id', $tag_id)->pluck('file_id')->count();
                $file[] = $fileCount > 0 ? File_Tag::where('tag_id', $tag_id)->pluck('file_id') : null;
            }
            $this->AdminFiles = File::whereIn('id', $file)->get();
        } else if ($langs != 0) {
            $this->files = File::where('language_id', $langs)->get();
        } else {
            $this->AdminFiles = File::all();
        }

合作伙伴过滤器的代码(工作)

代码语言:javascript
运行
复制
if($tag_id != 0 && $langs != 0)
        {
            for ($i = 0; $i < count($file_role); $i++) {
                $fileCount = File_Tag::where('tag_id', $tag_id)->where('file_id', '=', $file_role[$i])->pluck('file_id')->count();
                $file[] = $fileCount > 0 ? File_Tag::where('tag_id', $tag_id)->where('file_id', '=', $file_role[$i])->pluck('file_id') : null;
                }
                $this->files = File::whereIn('id', $file)->where('language_id', $langs)->get(); 
        }
        
       else if ($tag_id != 0) {                
                for ($i = 0; $i < count($file_role); $i++) {
                    $fileCount = File_Tag::where('tag_id', $tag_id)->where('file_id', '=', $file_role[$i])->pluck('file_id')->count();
                    $file[] = $fileCount > 0 ? File_Tag::where('tag_id', $tag_id)->where('file_id', '=', $file_role[$i])->pluck('file_id') : null;
                    }
                    $this->files = File::whereIn('id', $file)->get();        
            
        }
       else if($langs != 0) {
            $this->files = File::whereIn('id', $file_role)->where('language_id', $langs)->get();

    }
        else {
            $this->files = File::whereIn('id',$file_role)->get();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-28 08:31:21

据我所见,您希望基于(1)给定的文件标记(如果存在)筛选文件,并根据(2)给定的一组语言(如果存在)对它们进行筛选。(3)角色部分对我来说不是很清楚,但我认为您也可以调整下面的代码以包含该约束。

每当您想根据条件在查询中应用约束时,都可以使用条件从句

这只是在满足条件时将约束应用于查询。

(1)基于角色标签的过滤。可以创建“文件”和“FileTag”之间的关系,然后使用该关系筛选表之间的值:

代码语言:javascript
运行
复制
$files = File::query()
    // This tells Laravel than whenever $tag_id != 0, will apply the filter
    ->when($tag_id != 0, function ($query) use ($tag_id) {
        $query->whereHas('tags', function ($query) use ($tag_id) {
            $query->where('id', $tag_id);
        }
    })
    ->get();

(2)然后,如果提供了一种语言,则也要应用过滤器:

代码语言:javascript
运行
复制
$files = File::query()
    ->when($langs, function ($query) use ($langs) {
        $query->where('language_id', $langs);
    })
    ->get();

最酷的是你可以把它们锁起来。所以在最后你可以做:

代码语言:javascript
运行
复制
$files = File::query()
    ->when($tag_id != 0, function ($query) use ($tag_id) {
        $query->whereHas('tags', function ($query) use ($tag_id) {
            $query->where('id', $tag_id);
        }
    })
    ->when($langs, function ($query) use ($langs) {
        $query->where('language_id', $langs);
    })
    ->get();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74232121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档