php Laravel 5.1 从空值创建默认对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38115899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:29:33  来源:igfitidea点击:

Laravel 5.1 Creating default object from empty value

phplaravellaravel-5laravel-5.1

提问by Kristin K

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

我正在使用 Laravel 5.1 PHP 框架。当我尝试更新我的记录时,出现错误:

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

“AdminController.php 第 108 行中的 ErrorException:从空值创建默认对象”。

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

编辑刀片.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

回答by Ivanka Todorova

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

问题是$product = Product::find($id);返回NULL. 添加检查:

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.

虽然这是您的更新方法,但您可能在为此方法构建 url 时遇到错误。您传递给这条路线的 ID 可能是错误的。

Your form actionhas an error:

您的表单action有错误:

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

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

注意花括号,Blade 的语法是{{ expression }},而不仅仅是{}. 所以id永远不会传递给product.update路由。只需将其更改为:

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

回答by Hiren Makwana

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

对于 laravel 中的更新实体,使用PUT方法而不是POST。更新表单方法并尝试。

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

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

回答by Samvedna

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

检查产品是否存在,然后进行更新表单将如下所示

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

$ sign was missing:)

$ 符号丢失:)