wherein - whereraw laravel




Laravel-Eloquent: Advanced Wheres (2)

Intento crear algo como esto usando Eloquent. Sin embargo, estoy teniendo problemas con la cláusula o.

SELECT * FROM table WHERE column1 = 1 AND column2 = 2 AND (column3 LIKE $search_term.'%' OR column4 LIKE $search_term.'%') ORDER BY column1;

Esto es lo que tengo:

$query = DB::table('table')
        ->where('column1', '=', 1)
        ->where('column2', '=', 2)
        ->orderBy('column1', 'asc'); 

$results = App\Table::where(function ($query) use ($search_term) {
            $query->where('column3', 'like', $search_term.'%');
        })->orwhere(function ($query) use ($search_term) {
            $query->where('column4', 'like', $search_term.'%');
        });

Su consulta debería verse así:

$query = DB::table('table')
    ->where('column1', 1)
    ->where('column2', 2)
    ->where(function ($q) use ($search_term) {
        $q->where('column3', 'like', $search_term.'%')
          ->orWhere('column4', 'like', $search_term.'%');
    })
    ->orderBy('column1', 'asc')
    ->get(); 

También puede usar la cláusula whereRaw :

App\Table::where('column1', 1)
   ->where('column2', 2)
   ->whereRaw("(column3 LIKE $search_term.'%' OR column4 LIKE $search_term.'%')")
   ->orderBy('column1', 'asc'); 




eloquent