1

I'm trying to make a 'edit' page for a 'bookshop'. Everything in the front-end seems perfect, but when I click edit after inputting data I get this error: "Creating default object from empty value"

I've tried adding a 'new' as I seen on other topics but it didnt help.

My controller:

function Update(Request $request){

    $entry = Book::find($request->route('id'));
    $entry->name = $request -> book_name;
    $entry->writer_name = $request->writer_name;
    $entry->isbn = $request->book_isbn;
    $entry->save();
    return redirect('/');
}

It gives error on this line

$entry->name = $request -> book_name;

My router for update

Route::post('/edit/update/{id}','EditController@Update');

My form

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

It is just a basic form that updates the database items. When I click 'edit' (button) after changing some values it gives me the 'Creating default object from empty value' error.

1 Answer 1

4
$request -> book_name;

should be

$request->book_name;

(without spaces)

I would also recommend using

$entry = Book::findOrFail($id); 

in case the id is not found. Also, I guess what you mean is:

function update(Request $request, $id){
    $entry = Book::findOrFail($id); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Now I get another error... The POST method is not supported for this route. Supported methods: GET, HEAD.:( I'm literally tired of errors
be sure to add the {{ csrf_field() }} inside your <form>, if not you will get a CSRF error.

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.