doumaji6215 2015-10-24 14:13
浏览 78
已采纳

Laravel错误:从空值创建默认对象

I'm trying to update a record in the database . I'm trying to update an already registered product, then I do the research and then create several objects to save , the problem that laravel returns an error mesage warning that I am trying to create an object with a null value , and I'm passing a value for he. Follows the code below

enter image description here

Model

enter image description here

I can't resolve this error, i need some help!!!

public function edit()
{
    $idProduct = Input::get('pid');

    $produto = ProductModel::where('id', $idProduct)->first();

    $nome = Input::get('eNome');
    $descricao = Input::get('eDescricao');
    $userID = Auth::user()->id;

    $produto->nome = $nome;
    $produto->descricao = $descricao;
    $produto->users_id = $userID;

    if (Input::hasFile('eFoto'))
    {
        $file = Input::file('eFoto');

        $fileNameUniq = uniqid();
        $destinationPath = public_path().'assets/admin/gallery/';


        $extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);

        $fileName = uniqid();
        if(move_uploaded_file($file, 'assets/admin/gallery/'.$fileName.'.'.$extension)){
            $produto->foto = $fileName.'.'.$extension;
        }

    }

    $produto->save();

    return Redirect::back()->with(['message' => 'Produto alterado com sucesso!']);
}

Routes:

Route::resource('/produto','admin\ProductController@index',['only' => 'index']);
Route::get('/produto/editar/ativar','admin\ProductController@activeProduct',['only' => 'activeProduct']);
Route::get('/produto/editar/desativar','admin\ProductController@disableProduct',['only' => 'disableProduct']);
Route::get('/produto/editar/{id}', array('as' => 'editProduct', 'uses' => 'Admin\ProductController@editProduct'));
Route::get('/produto/excluir','admin\ProductController@destroy',['only' => 'destroy']);
Route::any('/produto/add','admin\ProductController@store',['only' => 'store']);
Route::post('/produto/editar','admin\ProductController@edit',['only' => 'edit']);

View

<div class="modal">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">Editar produto</h4>
                  </div>
                  <div class="modal-body">
                    <link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel='stylesheet' />

                    <!-- The main CSS file -->
                    <link href="{{URL::asset('/')}}assets/admin/uploader/css/style.css" rel="stylesheet" />
                    <!--<form id="upload" method="post" action="app\controllers\admin\GalleryController@store" enctype="multipart/form-data">-->

                    {!! Form::open(['id' => 'product', 'method' => 'POST', 'enctype' => 'multipart/form-data', 'files' => true, 'action' => 'admin\ProductController@edit']) !!}
                        <div class="clearfix">
                            {!! Form::text('eNome', $eProduct->nome,['class' => 'form-control', 'required' => 'required']) !!}
                        </div>
                        <br>
                        <div class="row">
                            <div class="form-group foto col-xs-12">
                                <label>Foto atual: <a href="{{URL::asset('/')}}assets/admin/gallery/{{$eProduct->foto}}"><img src="{{URL::asset('/')}}assets/admin/gallery/{{$product->foto}}" width="100%" height="300px" class="user-image" alt="Imagem"/></a></label>
                                <br>
                                <label>Selecione uma nova foto:</label>
                                <input class="form-control" type="file" name="eFoto" value="" />
                            </div>
                        </div>
                        <br>
                        <div class="clearfix">
                            {!! Form::textarea('eDescricao', $eProduct->descricao,['class' => 'form-control', 'placeholder' => 'descricao', 'required' => 'required']) !!}
                        </div>
                        <br>

                   {!! Form::close() !!}
                  </div>
                  <div class="modal-footer">
                    <!--<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Fechar</button>-->
                     <a class="btn btn-success pull-left"  href="{{URL::to('/yziadmin/produto')}}">Fechar</a>
                    {!! Form::hidden('pid', $eProduct->id) !!}
                    {!! Form::submit('Salvar', ['name'=>'btnsave', 'class'=>'btn btn-success pull-right', 'form'=>'product']) !!}
                    <!--<button type="button" class="btn btn-primary">Finalizar</button>-->
                  </div>
                </div><!-- /.modal-content -->
              </div><!-- /.modal-dialog -->
            </div><!-- /.modal -->
  • 写回答

2条回答 默认 最新

  • douwang9650 2015-10-24 15:07
    关注

    put your

    {!! Form::close() !!}
    

    under your

    {!! Form::hidden('pid', $eProduct->id) !!}
    

    so it will be posted as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?