所以在我的 laravel 應用程式中,我為“檔案”欄位做了一個過濾器。
我為不同的角色(管理員,合作伙伴)制作了這個過濾器,首先我讓它在合作伙伴方面作業(這顯示了該角色的所有檔案(不是所有檔案)都可以訪問,這可以 100% 作業。但現在我想做它為管理員作業,(訪問所有檔案)但這不起作用,因為它總是回傳此錯誤:
SQLSTATE[HY093]: Invalid parameter number
SQL:
select * from `file` where `id` in (9, 11, 9, 11, 9, 11, 9, 11, 9, 11)
如您所見,有重復的條目。有沒有辦法忽略這個錯誤,只加載檔案。如果我將查詢復制粘貼到 SQL 本身中,它就可以作業。
管理過濾器的代碼:
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();
}
合作伙伴過濾器的代碼(作業)
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();
}
uj5u.com熱心網友回復:
據我所見,您希望根據 (1) 給定的檔案標簽(如果存在)過濾檔案,并根據 (2) 給定的一組語言(如果存在)過濾它們。(3) 角色部分對我來說不是很清楚,但我認為您可以調整以下代碼以包含該約束。
每當您想根據條件在查詢中應用約束時,都可以使用Conditional Clauses。
這只是在滿足條件時將約束應用于查詢。
(1) 根據角色標簽進行過濾。您可以創建 File 和 FileTag 之間的關系,然后使用該關系來過濾表之間的值:
$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) 然后,如果提供了語言,您還想應用過濾器:
$files = File::query()
->when($langs, function ($query) use ($langs) {
$query->where('language_id', $langs);
})
->get();
最酷的部分是您可以將它們鏈接起來。所以最后你可以這樣做:
$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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522367.html
標籤:phpsql拉拉维尔
上一篇:為什么PHP看不到上傳的檔案?
