-
Notifications
You must be signed in to change notification settings - Fork 5
Some sugar
Alex Morozov edited this page Jan 24, 2021
·
3 revisions
Just so as not to write too much 😤
// select * from `invoices` where `name` like '%foo%'
Invoice::like('name', 'foo')->get();
// select * from `invoices` where `name` like '%foo'
Invoice::likeLeft('name', 'foo')->get();
// select * from `invoices` where `name` like 'foo%'
Invoice::likeRight('name', 'foo')->get();
Imagine that you have a field in the database in the format of a string, and you need to sort by it like number. Of course you will cast it.
Some types for example: date, datetime, time, char, signed, unsigned, binary.
Invoice::castColumn('name', 'signed')->orderBy('name')->get();
or multiple
Invoice::castColumn(['name' => 'signed', 'name2' => 'signed'])->orderBy('name')->get();
If you have indexes on the table, then you can use force index.
Invoice::forceIndex('name')->orderBy('name')->get();