How to auto fill a fk in Laravel using session data?

1

I have two tables, posts and users . The posts table has a relationship with the users table through the user_id column.

The question is this: in the controller I'm using a merge to populate the user_id with the id of the user. I would like to know if this merge can be overridden, perhaps by using the user (belongsTo) method of the Post model. See the code:

Controller:

$request->merge(['user_id' => \Auth::user()->id]);
Post::create($request->all());

Model Post:

public function user()
{
    return $this->belongsTo('App\Models\User');
}
    
asked by anonymous 31.03.2017 / 19:38

0 answers