Laravel 5.1 Creating a default object from an empty value

I am using the Laravel 5.1 PHP framework. When I try to update my record, I get an error message:

"ErrorException in line AdminController.php 108: creating default settings object from an empty value."

I searched on google, but I can not find any results to solve my problem.

Routes

Route::get('/admin/no', ' AdminController@index '); Route::get('/admin/product/destroy/{id}', ' AdminController@destroy '); Route::get('/admin/new', ' AdminController@newProduct '); Route::post('/admin/product/save', ' AdminController@add '); Route::get('/admin/{id}/edit', ' AdminController@edit '); Route::patch('/admin/product/update/{id}', ' AdminController@update ') 

Admincontroller

  public function edit($id) { $product = Product::find($id); return view('admin.edit', compact('product')); } public function update(Request $request, $id) { $product = Product::find($id); $product->id = Request::input('id'); $product->name = Request::input('name'); $product->description = Request::input('description'); $product->price = Request::input('price'); $product->imageurl = Request::input('imageurl'); $product->save(); //return redirect('/admin/nฮฟ'); } enter code here 

edit.blade.php

 div class="panel panel-info"> <div class="panel-heading"> <div class="panel-title">Edit Product</div> </div> <div class="panel-body" > <form action="/admin/product/update/{id}" method="POST"><input type="hidden" name="_method" value="PATCH"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> enter code here 
+6
source share
3 answers

The problem is that $product = Product::find($id); returns NULL . Add check:

 if(!is_null($product) { //redirect or show an error message } 

Although this is your update method, you may have encountered an error creating the URL for this method. Perhaps this is an invalid identifier that you are going to this route.

Your action form has an error:

 <form action="/admin/product/update/{id}" method="POST"> 

Note curly braces, Blade syntax {{ expression }} , not just {} . Therefore, id never passed to the product.update route. Just change it to:

 <form action="/admin/product/update/{{$id}}" method="POST"> 
+4
source

The laravel uses the PUT POST method for the update object. refresh the form and try.

 <form action="/admin/product/update/{id}"> <input name="_method" type="hidden" value="PUT"> 
+1
source

check if the product exists, upgrade. The form will look like this:

 <form action="/admin/product/update/{{$id}}" method="POST"> 

The $ sign was missing :)

0
source

All Articles