Laravel 中具有多个条件的多个预加载

我正在使用 Laravel 5.7 版本。

我想对 where 子句使用多个预加载。

我简化了 laravel。

$query = Employee::with(['User','Company','Client'])
                  ->select(*)->whereRaw("(User.id <> 0 and Company.name LIKE %A%) OR Client.id <> 0)")

我正在使用预加载来避免 JOIN 语句。

但问题是错误消息说 User.id is unknown column in where 子句。

我相信另外两个也是未知的专栏。

任何想法,将不胜感激。


繁花如伊
浏览 127回答 1
1回答

qq_花开花谢_0

它看起来有点丑陋,但你可以使用闭包:$query = Employee::with('User', function ($inner) {&nbsp; &nbsp; $inner->where('id', '!=', 0);})->with(['Company', 'Client'])->where(function (Builder $sub) use($companyName) {&nbsp; &nbsp; $sub->where('Company.name', 'like', $companyName)&nbsp; &nbsp; &nbsp; &nbsp; ->orWhere('Client.id', '!=', 0);})->get();这将检索:employees where (user.id != 0 AND (company.name LIKE :companyName OR client.id != 0))为了修改这些约束的顺序,您需要根据您的要求移动闭包和约束,但据我所知,应该这样做。
打开App,查看更多内容
随时随地看视频慕课网APP