Я использую последнюю версию Laravel.
У меня есть 2 таблицы MySQL, называемые "authors"
и "articles"
.
У меня есть 2 автора, "John"
и "Sam"
, John
имеет 2 статьи, Sam
имеет 1, Статьи имеют столбец "user_id"
с ID
авторов.
Чтобы отобразить Authors
и их Articles
, мне нужно сделать 2 цикла
@foreach($authors as $author)
// Here the author names are displayed
{{$author->name}}
// I have to do this loop so I can show the articles of the Author
@foreach($author->article as $article)
<h3> {{$article->name}} </h3>
@endforeach
@endforeach
Если вам это нужно, вот мой автор Модель:
class Author extends Model
{
//
public function article(){
return $this->hasMany("App\Article", "user_id");
}
}
Я в основном спрашиваю, есть ли способ сделать это чище.