【问题标题】:Laravel eloquent relationship return empty arrayLaravel 雄辩关系返回空数组
【发布时间】:2018-06-19 22:18:44
【问题描述】:

我想显示一个链接、一个页面、一个标签和许多带有来自“users”的用户名的 cmets,但是 cmets 返回一个空数组。所有表都有“id”作为主键并自动递增。

控制器:

$link = Link::with('page', 'tag', 'comments.user')->where('friendly_url', $id)->first();
return view('site.link', compact('link'));

模型(链接)

class Link extends Model
{
public function page()
{
    return $this->belongsTo(Page::class);
}

public function tag()
{
    return $this->belongsTo(Tag::class);
}

public function comments()
{
    return $this->hasMany(Comment::class);
}   
}

模型(cmets)

class Comment extends Model
{

    public function user(){
        return $this->belongsTo(User::class);
    }
    }

查看:

    @foreach($link->comments as $comment)
    <li class="comment">
    <h1>{{ $comment->user->name . $comment->user->lastname }}</h1>
   <h2>{{ $comment->content }}</h2>
    @endforeach



dd($link);

0 => Link {#291 ▼
      #relations: array:3 [▼
        "page" => Page {#295 ▶}
        "tag" => Tag {#289 ▶}
        "comments" => Collection {#292 ▼
          #items: array:2 [▼
            0 => Comment {#299 ▶}
            1 => Comment {#301 ▶}
          ]
        }
      ]

已解决:

$link->getRelation('comments');

但我也想显示 userProfile,但返回 0...

控制器:

$link = Link::where('friendly_url', $id)->with('page', 'tag', 'comments.user', 'comments.userProfile')->first();

        dd($link);



#relations: array:3 [▼
"page" => Page {#296 ▶}
"tag" => Tag {#290 ▶}
"comments" => Collection {#293 ▼
  #items: array:2 [▼
    0 => Comment {#300 ▶}
    1 => Comment {#302 ▼
      #relations: array:2 [▼
        "user" => User {#305 ▶}
        "userProfile" => null
      ]

【问题讨论】:

  • comments 表中有link_iduser_id 吗?你试过Link::with('page', 'tag', 'comments', 'comments.user')吗?
  • 您的-&gt;where 子句中的$id 到底是什么?
  • 是的,表格有两个字段... $id = 'cars-more-expensive-in-2017'(友好的网址)...是的,我也尝试过这种方式...
  • 我在DD中编辑了结果,看那里,获得了cmets...
  • 0 => 链接 {#291 ▼ #relations: array:3 [▼ "page" => 页面 {#295 ▶} "tag" => 标签 {#289 ▶} "cmets" = > 收藏 {#292 ▼ #items: array:2 [▼ 0 => 评论 {#299 ▶} 1 => 评论 {#301 ▶} ] } ]

标签: laravel eloquent


【解决方案1】:

1、数据库:cmets表中必须有link_iduser_id

2, Controller : 你应该使用with() 函数来获取commentsuser

    $link = Link::with(['page', 'tag', 'comments' => function($query){
           $query->with('user')
    }])->where('friendly_url', $id)->first();

    return view('site.link', compact('link'));

您可以使用dd($link)查看关系数据

你可以试试。 你可以在下面评论让我支持你

【讨论】:

  • Link {#291 ▼ #relations: array:3 [▼ "page" => Page {#295 ▶} "tag" => Tag {#289 ▶} "cmets" => Collection { #292 ▼ #items: 数组:2 [▼ 0 => 评论 {#299 ▶} 1 => 评论 {#301 ▶} ] } ]
  • 返回cmets,但是在视图中,数组“@foreach($link->cmets as $comment)”是空的...
  • 为 $link->cmets 的 foreach() 提供的参数无效
  • 你能 dd($link->cmets) 吗?
  • dd($link->page) return Page {#295 ▼ #table: "pages" #connection: "mysql" #primaryKey: "id"
猜你喜欢
  • 2015-12-17
  • 2021-06-20
  • 2013-04-10
  • 2018-04-11
  • 2015-03-14
  • 2018-11-28
  • 2015-01-11
  • 2021-06-03
  • 2020-03-26
相关资源
最近更新 更多