简体   繁体   English

Laravel 关系不返回任何内容

[英]Laravel relations doesn't return anything

i have problem in laravel eloquent relationships.我在 laravel eloquent 关系中有问题。 there is 2 model in my application: article and category.我的应用程序中有 2 个 model:文章和类别。

article model:文章 model:

public function category()
{
    return $this->belongsToMany('App\Category');
}

category model:类别 model:

public function article()
{
    return $this->hasMany('App\Article');
}

the relation between this tow is hasMany (Category -> article) & belongsToMany (Article -> category) .这两条之间的关系是hasMany (Category -> article) & belongsToMany (Article -> category)

category will fetch by requested slug using this method at categoryController: category 将在 categoryController 中使用此方法通过请求的 slug 获取:

$category = Category::where('slug', '=', $slug)->get();

problem will be shown in view when i want to fetch articles from category and nothhing will return back:当我想从类别中获取文章并且什么都不会返回时,问题将显示在视图中:

@foreach ($category->article->all() as $article)
    {{ $article->name }}
@endforeach

and from @dd($category->article) we will get empty collection:@dd($category->article)我们将得到空集合:

Collection {#323 ▼
  #items: []
}

As @lagbox tried to highlight in a comment, for pivot tables, both relationships should be belongsToMany .正如@lagbox 试图在评论中强调的那样,对于 pivot 表,两个关系都应该是belongsToMany Inverse of hasMany is belongsTo . hasMany的倒数是belongsTo

If one article belongs to many categories, and one category can have many articles, then, ideally, there it should be a many-to-many relationship.如果一篇文章属于多个类别,而一个类别可以有很多篇文章,那么理想情况下,它应该是many-to-many关系。 Category model should have a belongsToMany relationship with Article model and vice versa.类别 model 应该与文章 model 具有belongsToMany关系,反之亦然。 Additionally, there should be a pivot table, article_category .此外,应该有一个 pivot 表article_category And as many have suggested, you can get articles the belongs to a category by using @foreach($category->articles as $articles)正如许多人所建议的那样,您可以使用@foreach($category->articles as $articles)获取属于某个类别的文章

You can read more about many to many here: https://laravel.com/docs/5.8/eloquent-relationships#many-to-many你可以在这里阅读更多关于多对多的信息: https://laravel.com/docs/5.8/eloquent-relationships#many-to-many

You can use @forelse blade directive like您可以使用 @forelse 刀片指令,如

@forelse ($category->article as $article)
    <li>{{ $article->name }}</li>
@empty
    <p>No articles</p>
@endforelse

You can check that here你可以在这里查看

You dont need to write $category->article->all() .你不需要写$category->article->all() $category->article itself will return all articles. $category->article本身将返回所有文章。 So just use like,所以就用like,

@foreach ($category->article->all() as $article)
  {{ $article->name }}
@endforeach

For eager loading articles, you can use with keyword对于急切加载的文章,您可以使用with关键字

$category = Category::with('article')->where('slug', '=', $slug)->get();

In your controller在你的 controller

$category = Category::with('article')->where('slug',$slug)->get();

In your blade.php file在你的blade.php 文件中

@foreach($category->article as $article)
    <li>{{ $article->name }}</li>
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM