32
loading...
This website collects cookies to deliver better user experience
$articles = Article::all();
return view('articles', compact('articles'));
@foreach($articles as $article)
<li>{{ $article->title }}</li>
<li>{{ $article->author->name }}</li>
@endforeach
<li>{{ $article->author->name }}</li>
you will back to the safe zone with just 1 executed query.SELECT * FROM `articles`
SELECT * FROM `authors` WHERE `id` = 1 LIMIT 1
SELECT * FROM `authors` WHERE `id` = 2 LIMIT 1
SELECT * FROM `authors` WHERE `id` = 3 LIMIT 1
--.....
SELECT * FROM `authors` WHERE `id` = 30 LIMIT 1
with
method will be appears and save your project's life.with
method in your controller and write this code:$articles = Article::with('user')->get();
return view('articles', compact('articles'));
eager loading
instead of lazy load of Laravel to get authors data just one query will be executed.SELECT * FROM `authors` WHERE `id` in (1, 2, 3, ..., 30)
$articles = Article::with(['author', 'publisher'])->get();
$articles = Article::with(['author.addresses' => function($query){
$query->where('active', true)->latest();
}]);