简体   繁体   English

如何在许多关系中访问所有表数据

[英]How to access all table data in many many relationship laravel

I have 3 tables. 我有3张桌子。

1.posts 1.posts

2.categories 2.categories

3.category_post. 3.category_post。

My Posts Tables here : 我的帖子表在这里:

+------------+------------------+------+-----+---------------------+----------------+
| Field      | Type             | Null | Key | Default             | Extra          |
+------------+------------------+------+-----+---------------------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| title      | varchar(255)     | NO   |     | NULL                |                |
| slug       | varchar(255)     | NO   |     | NULL                |                |
| reporter   | varchar(255)     | NO   |     | NULL                |                |
| meta       | varchar(255)     | NO   |     | 0                   |                |
| body       | text             | NO   |     | NULL                |                |
| image      | varchar(255)     | NO   |     | 0                   |                |
| top        | tinyint(1)       | NO   |     | 0                   |                |
| post_count | int(11)          | NO   |     | 0                   |                |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+------------+------------------+------+-----+---------------------+----------------+

My Categories Table Here: 我的类别表在这里:

+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| name          | varchar(255)     | NO   |     | NULL                |                |
| category_slug | varchar(255)     | NO   |     | NULL                |                |
| parrent_id    | int(11)          | NO   |     | 0                   |                |
| created_at    | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at    | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+---------------+------------------+------+-----+---------------------+----------------+

My category_post table: 我的category_post表:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| category_id | int(10) unsigned | NO   | MUL | NULL    |                |
| post_id     | int(10) unsigned | NO   | MUL | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

I can create post with multiple category selected . 我可以选择多个类别来创建帖子。 But When i want to showing post by category, then i can't access category table . 但是,当我想按类别显示帖子时,则无法访问类别表。 That means, i want to view category name with each post . 这意味着,我想在每个帖子中查看类别名称。

Here is my Models: 这是我的模特:

In Category Model: 在类别模型中:

public function posts()
{
    return $this->belongsToMany('Post');
}

In Post Model: 在后期模型中:

public function categories()
{
    return $this->belongsToMany('Category');
}

My Query Helper function is for Post by category: 我的查询助手功能用于按类别发布:

public static function cat_post($category, $limit, $top)
{
    $posts = Post::whereHas('categories', function($q) use ($category, $top)
        {
            $q->where('name', 'like', $category);
            $q->where('top', 'like', $top);

        })->with('categories')->take($limit)->orderBy('created_at', 'DESC')->get();
    return $posts;
}

I can view all post data . 我可以查看所有发布数据。 But category name not . 但是类别名称不是。

My Post Loops: 我的帖子循环:

<?php $headline = Helper::head_post(10, 1); ?>

  @foreach ($headline as $post)
    <li><a href="">{{ $post->title }}</a></li>
  @endforeach

when i try this for category name not working: 当我尝试使用类别名称无法正常工作时:

  @foreach ($headline as $post)
    <li><a href="">{{ $post->categories->name }}</a></li>
  @endforeach

Please help me. 请帮我。

Because it's a many to many relation $post->categories->name will not work. 因为这是多对多关系,所以$post->categories->name无效。 It doesn't know which category (of the many) you want the name of. 它不知道您要使用哪个类别的名称。

So you could for example use $post->categories()->first()->name to get the name of the first category or loop over them to get the name of all categories. 因此,例如,您可以使用$post->categories()->first()->name来获取第一个类别的名称,或者在它们上循环以获取所有类别的名称。

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

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