【问题标题】:trying to search in a table or in its relation in laravel尝试在 laravel 中的表或关系中搜索
【发布时间】:2021-12-15 12:17:47
【问题描述】:

我需要在预约表中搜索代码或作为预约关系的患者姓名。这是我到目前为止达到的代码,但它不起作用:

$lab = Lab::with(['patient' => function ($q) use ($search_query) {
        $q->select('id', 'avatar', DB::raw('CONCAT(first_Name, " ", second_Name) AS name')
            ->where('name', 'like', "%{$search_query}%")
            ->orWhereRaw("concat(first_name, ' ', second_name) like '%$search_query%' ")
        );
    }])
        ->select('id', 'code')
        ->Where('code', 'like', "%{$search_query}%")
        ->limit(5)
        ->get();

【问题讨论】:

  • 约会表中没有选择

标签: php mysql sql laravel eloquent


【解决方案1】:

加载关系不同于在其上应用“where”,您应该加载关系并在另一个语句中应用“whereHas”:

 $lab = Lab::with(['patient' => function ($q) {
            $q->select('id', 'avatar', DB::raw('CONCAT(first_Name, " ", second_Name) AS name')
            );
        }])
            ->select('id', 'code')
            ->Where('code', 'like', "%{$search_query}%")
            ->orWhereHas('patient',function ($query) use ($search_query) {
                $query->where('name', 'like', "%{$search_query}%");
            })
            ->limit(5)
            ->get();

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2015-06-11
    • 1970-01-01
    • 2019-02-04
    • 2018-11-09
    相关资源
    最近更新 更多