Laravel: General Tips - maple-dev-team/docs GitHub Wiki

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
⚠️ **GitHub.com Fallback** ⚠️