samedi 18 août 2018

laravel update only reference table

I am successfully able to insert using Laravel eloquent one-to-many relationship. as you can see the below code.

The Code

$post = new Post;
$post->title = "my title";
$post->description = "my description";
$post->save();

$comment = new Comment;
$comment->author = "my author";
$comment->comment = "my explain";

// option 1:
// this will set the user_id on the role, and then save the role to the db
$post->comments()->save($comment);

How I can UPDATE comments table records ONLY using Laravel eloquent one-to-many relationship?

I have tried the following code to update but it is adding new row in comments table.

$post = Post::find(18);

$comment = new Comment;
$comment->author = "my author";
$comment->comment = "my explain";    

$post->comments()->save($comment);

Please share some guidelines. Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire