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();
Related
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.
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.
How can I create a query like
WHERE DATE(created_date) = '2014-05-26'
in Laravel?
I got this
->where('created_date', Input::get('date'))
but it doesn't work because created_date is datetime
By using the DateTime class maybe :
->where('created_date', new DateTime(Input::get('date')))
It should work!
Try this
where('created_date', '>', $date->startOfDay()->toDateTimeString())
->where('created_date', '<', $date->endOfDay()->toDateTimeString())
->get();
and take a look at this:
Laravel eloquent get model on date
You have to convert you string representation of the date into a Carbon instance.
$date = Carbon\Carbon::parse(Input::get('date'));
$results = Model::where('created_date', $date);
Happy coding!
I think the best option, and most logical one, is the following:
$results = Model::whereDate('created_date', Input::get('date'));
Use of Carbon is optional here.
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');
I want to return all of the rows in my database table that are a day or less old. I'm using Laravel 4. This is what I tried:
$date = date('Y-m-d H:i:s');
return MainContact::where(DATEDIFF('timestamp', $date), '<=', 1)->get();
This doesn't work. I read the documentation and it doesn't seem like you can pass Laravel MySQL functions. timestamp is a datetime field. How can I compare these dates in Laravel 4?
The answer that user1977808 gave you is not good because MySQL can't use an index on the timestamp column, since it has to compute an output of the DATE_SUB function for every row. Avoid such queries, they have to process the entire table every time!
How about something like this:
return MainContact::where('timestamp', '>=', time() - (24*60*60))->get();
I put the >= in there because you said "a day or less old", so they must have timestamp that is later than yesterday.
Alternatively,
You can use Carbon API that bundle with Laravel.
ModelName::where( 'timestamp', '>=', Carbon::now() )->get();
Reference: http://laravel.com/docs/5.1/eloquent-mutators
You could also use whereDate(), whereDay(), whereMonth() and whereYear(). In this case, whereDate() could be used as such, with Carbon's easy date functions:
return MainContact::whereDate('dateField', '<', Carbon::now()->subDay())->get();
return MainContact::where('timestamp', '>=', time() - (24*60*60))->get();
You can also do a raw query by using:
$results = DB::query( 'query' );
You only don't the the model object back in the results var