0

Facing this error message but not sure what is wrong with it? Tried the solutions at StackOverflow and other forums but didn't work either.

The error is at line $review->title=$request->title;

    public function updateReview(Request $request)
    {
    // dd($request->all());

    $review = Review::find($request->id);
    $review->id=$request->id;
    $review->title=$request->title;
    $review->review=$request->review;
    $review->rating=$request->rating;

    $review->save(); 

    return redirect('/');
    
    }

A dd($request->all()); returns the following:

array:5 [▼
  "id" => "3"
  "title" => "User 3 has updated title"
  "review" => "User 4 just updated review"
  "rating" => "5"
  "_token" => "pGFVAzHNg7HmXbkMXylxcM6biqaGnwFmsxjsrTgl"
]

And these are my routes:

Route::get('/edit_review/{id}', 'App\Http\Controllers\UserController@editReview');
Route::post('/update', 'App\Http\Controllers\UserController@updateReview')->middleware('user');
  • What is the output of `dd($request->all());`? – STA Nov 10 '20 at 08:40
  • array:4 [▼ "title" => "This is an updated Title" "review" => "I am updating the Review" "rating" => "5" "_token" => "Dyjm8Tis2MhpMsjKUkMI8Tfxv3rC6DK2OobMwmK7" ] – sufyanahmad Nov 10 '20 at 08:45
  • Please post code as text with the appropriate markup rather than images. – Nigel Ren Nov 10 '20 at 08:49
  • The error is not about what $request contains, but about $review not being an object in the first place - https://stackoverflow.com/questions/8900701/creating-default-object-from-empty-value-in-php – CBroe Nov 10 '20 at 09:06
  • this line cause the error `$review->title` because find method `find($request->id)` not matched with your query, so us `findOrFail()` instead of `find()` – STA Nov 10 '20 at 09:09
  • 1
    I already posted an answer about the error, because `$request->id` returns null. if you see the `dd($request->all());` output, there are no any `id` on the output. So there is no `$request->id` – STA Nov 10 '20 at 09:21
  • there is a `$id` you passed, can you post the route code here, so that I can fix your code – STA Nov 10 '20 at 09:22
  • @sufyanahmad hello, are you still facing the same issue? – STA Nov 11 '20 at 04:49
  • @sta So far it's ok. Many thanks for the help. – sufyanahmad Nov 11 '20 at 15:57
  • @suftanhmad please mark this,answer as accepted if it helped – STA Nov 11 '20 at 16:39

1 Answers1

0

You're getting this error Warning: Creating default object from empty value because $review->title returns false.

find() takes an id and returns a single model. If no matching model exist, it returns null.

You can use findOrFail() instead of find(), if the record not matched then findOrFail() will throw a 404 error :

$review = Review::findOrFail($request->id);

I think you've missed to define a parameter on your function, that passes with your route, probaby its {id} :

public function updateReview(Request $request, $id)
{  
    $review = Review::findOrFail($id); 
    //... 
}
STA
  • 30,729
  • 8
  • 45
  • 59