Does someone know how to speed up such a query? When loading <3000 items it takes like ages until I get a result.
$data = Data::select('updated_at', 'data')
->where('device', $request->id)
->where('type', $request->type)
->get();
Just search the word Paginate and read laravel documentation. You don't get all data and show it, you paginate them.
Related
I am on laravel, I am developing a library. I would like to display a list of books with a publication date of less than 3 months. Could someone help me build the elequent query. I tried with the DB class but it doesn't work.
$today = date('Y-m-d');
$books = DB::select("SELECT * from books where(DATEDIFF($today, 'publication_date') <=90)");
You can benefit from laravel query builder and pass now()->subDays(90) as the point that the date must be greater than.
DB::table('books')
->whereDate('publication_date', '>', now()->subDays(90))
->get();
The carbon package that comes with Laravel is quite handy for these types of thing.
What you could do is the following:
$books = Book::where('published_date', '<=', now()->addMonths(3))->get();
I am using Laravel-5.8 Eloquent to run a query. I want to get the total trucks utilized by a particular client for all their trips without counting a truck twice. I used the query below, but there is repetition of trucks used more than once:
$truckcount = Trip::where('client_id', $userClientId)
->select(\DB::raw("COUNT(truck_no) as count"))
->distinct()
->get();
How do I write the correct Eloquent query to achieve this?
try this:
$truckcount = Trip::where('client_id', $userClientId)
->select(\DB::raw("COUNT(DISTINCT truck_no) as count"))
->get();
I need sorting the Records data according to Relationship.
I am trying below Query.
$data = Lead::with('bdm', 'status_code', 'bdm.bdm')->get()->sortByDesc('bdm.bdm.name');
It works fine but I need data with pagination which is giving by Laravel 5 by default.
So If I am trying with below query . It is giving error.
$data = Lead::with('bdm', 'status_code', 'bdm.bdm')->pagination(20)->sortByDesc('bdm.bdm.name');
I am trying an other way to do the same task. It works fine but it is not sorting the records.
$data = Lead::with(['bdm','status_code', 'bdm.bdm' => function ($query) {
$query->orderBy('name', 'desc');
}])->paginate(20);
So kindly can anyone give me solution how to adjust this query.
Any Help will be appreciated.
Thanks
You should be using a join statement to do that.
If I were to assume your column names it should look something like this:
$data = Lead::with(['status_code', 'bdm.bdm'])
->join('bdms', 'bdms.id', '=', 'leads.bdm_id')
->orderBy('bdms.name', 'desc')
->paginate(20);
And the first bdm parameter in your query is redundant. It will be handled during bdm.bdm anyway.
Hi I have a query that looks roughly like this
Comment::join('users', 'users.id', '=', 'comments.user_id')
->whereIn('comments.id', $ids)
->paginate(5);
where $ids is an array of comment ids. Changing the paginate to get() works but I want to use paginate as it returns many built-in useful results such as next_page_url etc. So how do I modify the query to utilize both whereIn and paginate together?
I found out the issue later on, I forgot to serialize the object before I return the result. A simple toArray() has done the trick.
I was searching for making a GROUP BY name Eloquent ORM docs but I haven't find anything, neither on google.
Does anyone know if it's possible ? or should I use query builder ?
Eloquent uses the query builder internally, so you can do:
$users = User::orderBy('name', 'desc')
->groupBy('count')
->having('count', '>', 100)
->get();
Laravel 5
WARNING: As #Usama stated in comments section, this groups by AFTER fetching data from the database. The grouping is not done by the database server.
I wouldn't recommend this solution for large data set.
This is working for me (i use laravel 5.6).
$collection = MyModel::all()->groupBy('column');
If you want to convert the collection to plain php array, you can use toArray()
$array = MyModel::all()->groupBy('column')->toArray();
try: ->unique('column')
example:
$users = User::get()->unique('column');