创建表之间的联接Laravel

创建表之间的联接Laravel,laravel,join,Laravel,Join,我有两个表:Posts-Comment 我想建立这两个表之间的关系,并需要更新具有“1”Post_ID的注释: 路线: 路由::get('join','JoinController@Join'); 控制器: public function Join() { $Comment = Posts::find(1)->Comment; $Comment->Title = "JOIN"; $Comment->save(); } 职位模式: public func

我有两个表:Posts-Comment

我想建立这两个表之间的关系,并需要更新具有“1”Post_ID的注释:

路线:

路由::get('join','JoinController@Join');

控制器:

public function Join()
{
    $Comment = Posts::find(1)->Comment;
    $Comment->Title = "JOIN";
    $Comment->save();
}
职位模式:

public function Comment()
{
    return $this->hasOne('App\Comment');
}
注释模型:

public function Posts()
{
    return $this->belongsTo('App\Posts');
}
但我收到了这个错误:

正在尝试获取非对象的属性

改为这样做:

$comment = new Comment;
$comment->Title = 'JOIN';
#comment->post_id = 1;
$comment->save();
或者您可以使用
create()
方法:

$Comment = Posts::find(1)->Comment()->create(['Title' => 'JOIN']);

如果要使用
create()
方法,请确保
Title
位于
$filleble
数组中。

$Comment=Posts::find(1)->Comment()->first()兄弟,这是不工作的你有任何评论与该职位id??尝试dd($comment)并显示ushe所说的更新,所以我猜会有一些注释,在非-object@Mohammad这意味着在DB中没有ID=1的post
$Comment = Posts::find(1)->Comment()->first();
$Comment->Title = "JOIN";
$Comment->save();