首页 > 解决方案 > 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);

标签: phpmysqlarrayslaravel

解决方案


推荐阅读