Eloquent Inverse Relationship

Update your Post model by adding this:

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

We can now display the post’s user in our routes:

use App\Models\User;

/*
|--------------------------------------------------------------------------
| ELOQUENT Relationships
|--------------------------------------------------------------------------
*/

Route::get('/post/{id}/user', function($id){
    return Post::find($id)->user->name;
});

View in your browser.

Leave a Reply

Your email address will not be published. Required fields are marked *