Home > Software engineering >  Laravel return value of updating a model
Laravel return value of updating a model

Time:11-28

what I want to do is when the update is done, the data of the whole Model is returned.

I am registering and updating the database.

what i want when the data is updated i want it to return me all the data.

return response()->json($category);

the return returns 1, what I want to do is show the updated data instead of 1

$category = Category::where('id', $id)->update([
            'title' => $request->title,
            'slug' => $request->slug,
            'is_home' => $request->is_home,
            'language' => $request->language,
            'status' => $request->status,
        ]);
        return response()->json($category);

CodePudding user response:

Create an instance of your object Category and save, that should work

$category = Category::find($id);
$category->title = $request->title;
$category->slug = $request->slug;
$category->is_home = $request->is_home;
$category->language = $request->language;
$category->status = $request->status;
$category->save();
return response()->json($category);

The return of the update() method is a boolean, not the object.

CodePudding user response:

    $category = Category::where('id', $id)->update([
            'title' => $request->title,
            'slug' => $request->slug,
            'is_home' => $request->is_home,
            'language' => $request->language,
            'status' => $request->status,
        ]);
    if(isset($category)){
     $category = Category::where('id', $id)->first()
     return response()->json($category);
}
    
  • Related