laravel get query code example

Example 1: print last sql query laravel

DB::enableQueryLog(); // Enable query log

// Your Eloquent query executed by using get()

dd(DB::getQueryLog()); // Show results of log

Example 2: show query in laravel

Use the toSql() method on a QueryBuilder instance.

DB::table('users')->toSql()

Example 3: laravel get query parameters

$name = $request->query('name', 'Helen');

Example 4: laravel find query

return Destination::orderByDesc(
    Flight::select('arrived_at')
        ->whereColumn('destination_id', 'destinations.id')
        ->orderBy('arrived_at', 'desc')
        ->limit(1)
)->get();

Example 5: laravel find query

$model = App\Models\Flight::where('legs', '>', 100)->firstOr(function () {
        // ...
});

Tags: