Sunday, 18 April 2021

Laravel eloquent orderByRaw, WHERE With function, WhereIn, OrWhere

 orderByRaw

https://stackoverflow.com/questions/29659430/mysql-order-by-field-in-eloquent

 $facilities = $query->with(['interest','city:id,name', 'state:id,name'])
        ->Active()
        ->whereIn('facility_id', $facilities_list)
        ->orderByRaw('FIELD(facility_id, '.implode(", " , $facilities_list).')')
        ->get();

https://stackoverflow.com/questions/17006309/how-to-use-order-by-for-multiple-columns-in-laravel-4

Using order by twice:

MyTable::orderBy('coloumn1', 'DESC')
    ->orderBy('coloumn2', 'ASC')
    ->get();

Laravel Where
https://laravel.com/docs/5.7/queries#parameter-grouping
Laravel where with condition :
DB::table('users')
            ->where('name', '=', 'John')
            ->where(function ($query) {
                $query->where('votes', '>', 100)
                      ->orWhere('title', '=', 'Admin');
            })
            ->get();
Laravel where and
https://stackoverflow.com/questions/33816591/and-statement-in-laravel-5-eloquent
Model::where('name', '=', 'jason')
     ->where('age', '=', 16, 'or')
     ->get();


Laravel OrWhere:
https://stackoverflow.com/questions/33816591/and-statement-in-laravel-5-eloquent
Model::where('name', '=', 'jason')
     ->orWhere('age', '=', 16)
     ->get();

whereIn
$users = DB::table('users')
                    ->whereIn('id', [1, 2, 3])
                    ->get();


No comments:

Post a Comment