10

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

"ErrorException in AdminController.php line 108: Creating default object from empty value".

I have searched in google but I can't 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

5 Answers 5

9

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

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

Though this is your update method, so probably you're having an error while building the url for this method. It might be a wrong id you're passing to this route.

Your form action has an error:

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

Notice the curly braces, Blade's syntax is {{ expression }}, not just {}. So id is never passed to the product.update route. Just change it to:

<form action="/admin/product/update/{{$id}}" method="POST">
Sign up to request clarification or add additional context in comments.

3 Comments

in edit.blade.php, shouldn't it be {{$id}} you forgot the $
I make changes to form action but nothing, I get new error NotFoundHttpException in RouteCollection.php line 161:
@KristinK Just add this to your form: <input name="_method" type="hidden" value="PUT">.
1

check if the product exists then do the update The form will look like this

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

$ sign was missing :)

Comments

1

For update entity in laravel uses PUT method not POST. update form method and try.

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

<input name="_method" type="hidden" value="PUT">

1 Comment

There's no method="PUT" in HTML forms. Only GET and POST.
0

Check your web.php file maybe mistake your controller name.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

This may be helpful for similar issues for others..

Instead of $product = Product::find($id); use $product = Product::where('some_other_id', $id);. I am saying this because you might be giving reference to some_other_id like a foreign key instead of primary key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.