一对多关系没有返回我期望的结果

时间:2019-03-31 12:13:42

标签: laravel

我已将评论集成到我的帖子中。当我发布评论时,我尝试通过使用关系来获取用户的评论名称。但是,它返回一个User模型,而不是特定的User属性本身。

我尝试使用dd()转储该变量,以查看问题所在,但似乎无法解决。

CommentUser.之间的关系

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

public function author()
{
  return $this->belongsTo('App\User');
}


public function specific_post(Post $post){
    $comments = Comment::where('post_id',$post->id)->get();
    dd($comments[0]->author());

    return view('MainPages/specific_post' , compact('post' , 'comments'));
}

转储变量将返回以下内容:

BelongsTo {#241 ▼
  #child: Comment {#236 ▶}
  #foreignKey: "author_id"
  #ownerKey: "id"
  #relationName: "author"
  #query: Builder {#232 ▶}
  #parent: Comment {#236 ▼
    #fillable: array:3 [▶]
    #casts: array:2 [▶]
    #hidden: array:1 [▶]
    #connection: "mysql"
    #table: "comments"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:6 [▼
      "id" => 1
      "user_id" => 1
      "post_id" => 1
      "content" => "Hello , i really like your content!!"
      "created_at" => "2019-03-28 20:56:01"
      "updated_at" => "2019-03-28 20:56:01"
    ]
    #original: array:6 [▶]
    #changes: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #visible: []
    #guarded: array:1 [▶]
  }
  #related: User {#235 ▼
    #fillable: array:3 [▼
      0 => "name"
      1 => "email"
      2 => "password"
    ]
    #hidden: array:2 [▼
      0 => "password"
      1 => "remember_token"
    ]
    #casts: array:1 [▼
      "email_verified_at" => "datetime"
    ]
    #connection: "mysql"
    #table: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #changes: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #visible: []
    #guarded: array:1 [▶]
    #rememberTokenName: "remember_token"
  }
  #withDefault: null
}

如您所见,没有特定用户的属性,我似乎找不到问题。

1 个答案:

答案 0 :(得分:2)

这是因为您要放弃IP方法,该方法将返回关系模型(在这种情况下,该模型不返回任何数据)。 而是转储from tkinter import * class Window: def __init__(self,root): self.frame = Frame(root) self.frame.pack(side = LEFT) self.frame1 = Frame(root) self.frame1.pack() self.But = Button(self.frame,text = 'Press', command = self.ButOk) self.But.pack(side = LEFT,anchor = E) def ButOk(self): self.Aux = False self.Aux = not self.Aux if self.Aux: Window1(root) class Window1: def __init__(self,root): self.fram = Frame(root) self.fram.pack(side = LEFT) self.fram1 = Frame(root) self.fram1.pack() self.But = Button(self.fram, text = 'X',command = self.close) self.But.pack(side =RIGHT,anchor = W) def close(self): self.Aux1 = False self.Aux1 = not self.Aux1 if self.Aux1: self.fram.pack_forget() root=Tk() Window(root) root.mainloop() (看看是否没有括号?),您将获得数据。