php - MYSQL 查询中的数组问题 - Laravel
问题描述
Laravel 的 mysql 数据库查询问题。我希望该用户能够在搜索引擎中输入最多 3 个短语。输入它们后,脚本将在类型(名字、姓氏、职位描述等)的字段中扫描数据库并显示所有与查询匹配的人。问题来了。问题可能出在变量 $listAll 中的数组中。例如:变量 $listAll 为某些搜索显示这样的值: Array ([0] => 0 [1] => Array ([0] => 3 [1] => 5 [2] => 8 [3] => 11 [ 4] => 32))
并且数据库查询只向我显示了一个用户(ID 为 3 的用户 - 第一个在数组列表中)。好像这个查询没有循环或其他什么。
问题是 - 如何生成/处理 $listAll 变量以在数据库查询中显示 ID 在 $listAll 中的所有用户?
$listAll[] = 0;
$list[] = 0;
$categ[] = 0;
$categAll[] = 0;
$categUsers[] = 0;
foreach ($searchArray as $arr) {
if(strlen($arr) > 20){
return redirect('/baza');
}
$list[1] = Hashtags::Where('tag', $arr)->pluck('users_id')->all();
$list[2] = User::Where('name', 'LIKE', '%'.$arr.'%')->pluck('id')->all();
$list[3] = User::Where('surname', $arr)->pluck('id')->all();
$list[4] = User::Where('title', $arr)->pluck('id')->all();
$list[5] = User::Where('description', 'LIKE', '%'.$arr.'%')->pluck('id')->all();
$list[6] = User::Where('my_experience', 'LIKE', '%'.$arr.'%')->pluck('id')->all();
$list[7] = User::Where('education', 'LIKE', '%'.$arr.'%')->pluck('id')->all();
$list[8] = User::Where('profits_from', 'LIKE', '%'.$arr.'%')->pluck('id')->all();
$categ = Categories::where('name', 'LIKE', '%'.$arr.'%')->orWhere('subname', 'LIKE', '%'.$arr.'%')->pluck('id')->all();
if($categ)
array_push($categAll, $categ);
for($i = 1; $i < 9 ; $i++){
if($list[$i])
array_push($listAll, $list[$i]);
}
}
$categUsers = User::whereHas('categories', function($q) use ($categAll){$q->whereIn('categories_id', $categAll);})->pluck('id')->all();
$bazaAll = User::whereIn('id', $listAll)
->when(!empty($categUsers), function ($query) use ($categUsers){
return $query->orWherein('id', $categUsers);})
->whereHas('roles', function($q){$q->whereIn('roles_id', ['3']);})
->simplePaginate(10);
解决方案
推荐阅读
- python-2.7 - 如何在 VScode 中正确设置 PYTHONPATH 环境
- javascript - 将鼠标悬停在元素上的Javascript函数?
- java - 如何解析具有相同键的动态嵌套 JSON 并存储在 JAVA 类中
- git - 在两个目录之间创建 git diff 并将补丁应用于目录
- protractor - AWS Device Farm:Protractor 脚本在本地机器上运行,而不是在 AWS 云机器上运行
- flutter - 如何防止对象在每次被调用时重新初始化?- 飞镖/颤振
- r - 如何保持每行至少有两个值的数据
- python - 在 repli 中查找海龟屏幕的大小
- ruby-on-rails - 分组后如何在 Rails 中排名
- html - 我试图让我的图像在它们被悬停后改变,但是由于某种原因,当我将鼠标悬停在另一个变化上时