Skip to content

Some sugar

Alex Morozov edited this page Jan 24, 2021 · 3 revisions

Why is this needed?

Just so as not to write too much 😤

Usage

like

// 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();

casting

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();

force index

If you have indexes on the table, then you can use force index.

Invoice::forceIndex('name')->orderBy('name')->get();
Clone this wiki locally