2016-06-30 17 views
5

Laravel 5.1 PHP çerçevesini kullanıyorum. Benim kaydı güncelleştirmek için çalıştığımda, hatayı alıyorum: Ben google araştırdımLaravel 5.1 Boş değerden varsayılan nesne oluşturma

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

ama benim sorunu çözmek için herhangi bir sonuç bulamıyorum.

yolları

Route::get('/admin/no', '[email protected]'); 
Route::get('/admin/product/destroy/{id}', '[email protected]'); 
Route::get('/admin/new', '[email protected]'); 
Route::post('/admin/product/save', '[email protected]'); 
Route::get('/admin/{id}/edit', '[email protected]'); 
Route::patch('/admin/product/update/{id}', '[email protected]') 

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 

cevap

4

sorun t, $product = Product::find($id); numaralı hat NULL değerini döndürür. Bu güncelleme yöntemi olmasına rağmen

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

, bu nedenle bu yöntem için url inşa ederken muhtemelen bir hata yaşıyorsanız: çek ekleyin. Bu rotaya geçtiğiniz yanlış bir kimlik olabilir.

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

Bildirimi küme parantezleri, Blade'in sözdizimi {{ expression }}, sadece {} geçerli:

Formunuz action bir hata var. id, product.update rotasına asla geçmez. Ürün daha sonra güncelleme formu bu

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

$ işareti güncelleme varlık için :)

+2

edit.blade.php içinde, bu olmamalı '{{$ id}}' sen Doğru konum '$' –

+0

unuttum. –

+0

Eylem oluşturmak için değişiklikler yapıyorum ama hiçbir şey, RouteCollection.php satırında NotFoundHttpException yeni hata alıyorum 161: –

0

çek: Sadece olarak değiştirin laravel'de PUT yöntemi POST kullanır. form yöntemini güncelle ve dene.

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

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

eksikti gibi görünecektir do varsa

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

HTML formlarında 'method =" PUT "' yoktur. Sadece 'GET' ve' POST'. –

+0

evet, cevabımı güncelledim. –