Home > Software design >  How to return back the full model object after create
How to return back the full model object after create

Time:01-09

I have posts table containing id , name, status, desc, status is enum('complete','incomplete') when I create a new post like this:

CreatePostRequest contains:
'name' , 'desc'

// create method
public function __invoke(CreatePostRequest $request)
{ 
   $post = Post::create($request->validated());
   dd($post);
}
output:
  #attributes: array:10 [
    "name" => "test"
    "desc" => "desc"
  ]

How can make status return with object after created!

I know the way of megre_array, but I do not thing this is a good idea espleacly when I have a lot of columns in table!

So is there any other way to return full of elequent object!

CodePudding user response:

You can call refresh on that Model instance to reload it with fresh attributes from the database:

return $post->refresh();

refresh will refresh the attributes of the current instance and return the current instance.

Laravel 8.x Docs - Eloquent - Retrieving Models - Refreshing Models refresh

  •  Tags:  
  • Related