从Laravel中的DB :: table查询中获取子对象

时间:2018-01-04 10:28:25

标签: laravel

我有3个表:帖子,标签和Post_tag。最后一个只是一个数据透视表:

文章:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title       | varchar(60)      | NO   |     | NULL    |                |
| created_at  | timestamp        | YES  |     | NULL    |                |
| updated_at  | timestamp        | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

标签:

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name       | varchar(50)      | NO   |     | NULL    |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

Post_tag:

+------------+------------------+------+-----+---------+-------+
| Field      | Type             | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| post_id    | int(10) unsigned | YES  | MUL | NULL    |       |
| tag_id     | int(10) unsigned | YES  | MUL | NULL    |       |
| created_at | timestamp        | YES  |     | NULL    |       |
| updated_at | timestamp        | YES  |     | NULL    |       |
+------------+------------------+------+-----+---------+-------+

模型是正确的。

问题:

我在posts / index.blade.php文件中收到此错误:

Undefined property: stdClass::$tags
(View: D:\laragon\www\laravel\resources\views\posts\index.blade.php)

在posts / index.blade.php中,我在@foreach ($posts as $post)中有@foreach列出帖子所属的标签。

@foreach($post->tags as $tag)
    <a class="btn btn-sm btn-info" href="/tag/{{ $tag->id }}">
        <span>{{ $tag->name }}</span>
    </a>
@endforeach

以前的@foreach仅在控制器发送$posts = Post::all()

时有效

但是我只能向视图发送一些具有某些条件的行(已发布,作者ID等)。所以我在控制器中有这个:

$posts = DB::table('posts')
                ->orderBy('posts.created_at', 'desc')
                ->where('town_id', '=', $request->session()->get('citySelected')->id)
                ->paginate(10);

我甚至尝试设置连接:

$posts = DB::table('posts')
                ->join('post_tag', 'posts.id', '=', 'post_tag.post_id')
                ->join('tags', 'post_tag.tag_id', '=', 'tags.id')
                ->orderBy('posts.created_at', 'desc')
                ->where('town_id', '=', $request->session()->get('citySelected')->id)
                ->paginate(10);

但是不起作用,让我觉得DB :: table只带来父对象而不是子对象。

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用with方法加载包含相关标签的帖子:

$posts = Post::with('tags')
    ->where('town_id', session('citySelected')->id)
    ->latest()
    ->paginate(10);

然后你就可以做到这一点:

@foreach ($posts as $post)
    @foreach($post->tags as $tag)
        <a class="btn btn-sm btn-info" href="/tag/{{ $tag->id }}">
            <span>{{ $tag->name }}</span>
        </a>
    @endforeach
@endforeach

答案 1 :(得分:0)

$ posts传递给index.blade.php是一个收集,而不是一个对象所以你的循环@foreach是错误的