php - Can I preload belongsTo parent models in Laravel 4? -
i'm getting big batch of 1000 records need loop through in view. them doing like:
$children = child::all(); and in view, this:
@foreach($children $child) {{ $child->parent->name }} @endforeach the problem is, executes 1000 queries. there way me create utilize of belongsto method relationship without massive loss of efficiency?
yes, should utilize eager loading using with:
$children = child::with('parent')->all(); now 1 query should launched parent relation.
php laravel eloquent
No comments:
Post a Comment