lundi 8 octobre 2018

Article, Comment and User relationship in Laravel 5.1

I made a blog where you can CRUD articles and post comments. Everything is fine and working good.

I just want to make Laravel to do the magic when the User posts a comment to an article and instead of hard-coding article_id and user_id.

Comment::create([
    'body' => request('body'),
    'article_id' => $article->id,
    'user_id' => Auth::User()->id]
);

Is it possible to use Laravel's Eloquent relationships to chain up some functions/methods and simplify it a little bit?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire