Skip to content

Laravel: General Tips

Oscar Fabiano edited this page Jul 6, 2021 · 1 revision

Lazy Load

  • IF YOU ARE NOT INSIDE A RESOURCE, BECAUSE WILL A QUERY FOR EACH ROW
Instead of doing this:
$purchase = Purchase::where('id', $id)->with('details')->first();
$purchase->details

Do this:

$purchase = Purchase::where('id', $id)->first();
$purchase->details

Laravel will query the details when/if you need to use the details

Eloquent

Simplify with eloquent:

before:

$result = Transfer::where('id', $id)->get();
$result[0]

after:

$result = Transfer::find($id);
$result
https://laravel.com/docs/8.x/eloquent-collections#method-find
Clone this wiki locally