Answered by : suhail-khan
// Retrieve a model by its primary key...
$flight = App\Models\Flight::find(1);
// Retrieve the first model matching the query constraints...
$flight = App\Models\Flight::where('active', 1)->first();
// Shorthand for retrieving the first model matching the query constraints...
$flight = App\Models\Flight::firstWhere('active', 1);
Source : https://laravel.com/docs/8.x/eloquent | Last Update : Thu, 15 Oct 20
Answered by : suhail-khan
return Destination::orderByDesc( Flight::select('arrived_at') ->whereColumn('destination_id', 'destinations.id') ->orderBy('arrived_at', 'desc') ->limit(1)
)->get();
Source : https://laravel.com/docs/8.x/eloquent | Last Update : Thu, 15 Oct 20
Answered by : sikadner
$query = Author::query();
$query->when(request('filter_by') == 'likes', function ($q) { return $q->where('likes', '>', request('likes_amount', 0));
});
$query->when(request('filter_by') == 'date', function ($q) { return $q->orderBy('created_at', request('ordering_rule', 'desc'));
});
$authors = $query->get();
Source : https://laraveldaily.com/less-know-way-conditional-queries/ | Last Update : Thu, 22 Sep 22
Answered by : dfg
<?php
Route::get('games', function () { $games = DB::table('games')->get(); return view('games', ['games' => $games]);
});
Source : https://vegibit.com/how-to-use-the-laravel-query-builder/ | Last Update : Wed, 27 Jul 22
Answered by : samer-saeid
Visitor::select('country') ->withCount('id') ->groupBy('country') ->latest() ->get() //or Visitor::query()->groupBy('country')->orderByDesc('count')->select('country' ,DB::raw('COUNT(1) as count'))->get()
Source : | Last Update : Mon, 16 May 22
Answered by : suhail-khan
<?php
$flights = App\Models\Flight::all();
foreach ($flights as $flight) { echo $flight->name;
}
Source : https://laravel.com/docs/8.x/eloquent | Last Update : Thu, 15 Oct 20
Answered by : suhail-khan
use App\Models\Destination;
use App\Models\Flight;
return Destination::addSelect(['last_flight' => Flight::select('name') ->whereColumn('destination_id', 'destinations.id') ->orderBy('arrived_at', 'desc') ->limit(1)
])->get();
Source : https://laravel.com/docs/8.x/eloquent | Last Update : Thu, 15 Oct 20
Answered by : attractive-antelope-60qca537rcog
//select specified colomns from all users
Employee::get(['name','email','title']);
Source : | Last Update : Mon, 30 May 22