I would like to execute the follow sentence using laravel eloquent
SELECT *, count(*) FROM reserves group by day
The only solution occurs to me is to create a view in the DB, but I am pretty sure there is a way to do it in laravel way.
You could use this: $reserves = DB::table('reserves')->selectRaw('*, count(*)')->groupBy('day');
you can easily use it with laravel 6 and laravel 7 application. take() will help to get data from a database table with a limit. skip() will help to skip some records when you fetch data from the database table. So, let's see bellow examples that will help you how to use take() and skip() eloquent query in laravel.
If you want to use Eloquent, and query any entity to return only results where a relationship exists and meets criteria you can use the whereHas() method with a subquery. User::where('verified', 1) ->whereHas('role', function($query) { $query->whereIn('title', ['seller', 'admin']); })->get();
toArray is a model method of Eloquent, so you need to a Eloquent model, try this: User::where('name', '=', 'Jhon')->get()->toArray(); http://laravel.com/docs/eloquent#collections. Follow this answer to receive notifications.
You could use this:
$reserves = DB::table('reserves')->selectRaw('*, count(*)')->groupBy('day');
As you wish to do it with Laravel Eloquent I assume you have a model name Reserve
. In this case you can use this
$reserve = Reserve::all()->groupBy('day')->count();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With