我需要應用我在此代碼中執行的相同連接,但在另一個代碼中我使用 Eloquent\Builder $query 構建它
我想要的加入是這樣的:
$afiliates = DB::table('ad_afiliado as af')
->join('af_promocion as promo', 'af.Clave', '=', 'promo.id_afiliado')
->select('af.logo_url', 'af.NombreComercial', 'af.Destacado', 'af.id_afiliado', 'af.Clave', 'af.DestacadoInicio')
->where('promo.v_fin','>',$FechaActual)
->paginate(9);
我想加入的代碼是這樣的:
$afiliates = AdAfiliado::query()
->where('Activo','=', 'S')
->where(function (\Illuminate\Database\Eloquent\Builder $query) use ($request) {
$query->orWhere('NombreComercial', 'like', "%{$request->search}%");
$query->orWhere('Etiqueta', 'like', "%{$request->search}%");
$query->orWhere('Categoria', 'like', "{$request->search}");
})
->orderBy('CLAVE', $request->order)
->paginate(9);
我感謝您的幫助!
uj5u.com熱心網友回復:
您可以使用whereHas()AdAfiliado 和 AfPromocion 之間的關系
$afiliates = AdAfiliado::query()
->where('Activo','=', 'S')
->where(function (\Illuminate\Database\Eloquent\Builder $query) use ($request) {
$query->orWhere('NombreComercial', 'like', "%{$request->search}%");
$query->orWhere('Etiqueta', 'like', "%{$request->search}%");
$query->orWhere('Categoria', 'like', "{$request->search}");
})
->whereHas('afPromocions', function($afPromocion) use($FechaActual) {
$adPromocion->where('v_fin', '>', $FechaActual);
})
->orderBy('CLAVE', $request->order)
->paginate(9);
uj5u.com熱心網友回復:
I got solved, as I read in this site:
https://ashallendesign.co.uk/blog/using-query-in-laravel-eloquent-queries
query() 方法不是必需的,所以我將其洗掉,并以 DB::table 方式進行。
我還洗掉了路線 \Illuminate\Database\Eloquent\Builder
結果代碼是這樣的:
$afiliates = DB::table('ad_afiliado as af')
->join('af_promocion as promo', 'af.Clave', '=', 'promo.id_afiliado')
->select('af.logo_url', 'af.NombreComercial', 'af.Destacado', 'af.id_afiliado', 'af.Clave', 'af.DestacadoInicio')
->where('promo.v_fin','>',$FechaActual)
->where('af.Activo','=', 'S')
->where(function ($query) use ($request) {
$query->orWhere('af.NombreComercial', 'like', "%{$request->search}%");
$query->orWhere('af.Etiqueta', 'like', "%{$request->search}%");
$query->orWhere('af.Categoria', 'like', "{$request->search}");
})
->orderBy('af.CLAVE', $request->order)
->paginate(9);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532637.html
標籤:拉拉维尔加入雄辩
