通过ID在表单中加载数据,以便在laravel 5.2

时间:2016-08-05 03:59:26

标签: php mysql laravel-5.2

我正在尝试填充数据以编辑表单。这是我的模特

public function  EditBatch($id,$request){

  $data= DB::table('in_batch')
        ->where('id', $id)
        ->update(array(
            'id'=>$request->input('id'),
            'file_name' => $request->input('file_name'),
            'batch_type' => $request->input('batch_type'),
            'activity_type' => $request->input('activity_type'),
            'schedule_time' => $request->input('schedule_time'),
            'predecessor' => $request->input('predecessor'),
            'priority' => $request->input('priority'),
            'batch_remark'=>$request->input('batch_remark'),
            'approved_by' => Auth::user()->id,
            'approved_on'=>date("Y-m-d H:i:s"),
        ));
    return $data;
}

这是我的控制器

public function edit($id){
   $obatch = new BatchType();
   $batch_type = $obatch->GetBatchTypeDropDown();
   $batch = new ManageBatch();
   $batch->GetBatchById($id);
   return view('batch.edit', array('batch'=>$batch,'batch_type'=>$batch_type));
}

这是我的观点

{!! Form::open (array('url' => array('batch/update',$batch->id), 'class' =>  'form-horizontal', 'method' => 'post','id'=>'editbatch')) !!}

    <div class="form-group">
            {!! Form::label('batch_id', 'batch_id',array('class'=>'col-md-4 control-label')) !!}


        <div class="col-md-6">
            {!! Form::text('batch_id',$batch->id,array('class'=>'form-control','id'=>'batch_id')) !!}
        </div>

    </div>
{!! Form::close() !!}

当我尝试将数据加载到视图时,显示错误

  

未定义属性:App \ Models \ Batch \ ManageBatch :: $ id(查看:C:\ wamp \ www \ hutch-in-portal \ resources \ views \ batch \ edit.blade.php)

如何解决这个问题?

三江源

1 个答案:

答案 0 :(得分:0)

我发现了一个解决方案,错误在于控制器方法

公共功能编辑($ id)

{
    $obatch = new BatchType();
    $batch_type = $obatch->GetBatchTypeDropDown();
    $ouser = new ManageBatchUser();
    $batch_user = $ouser->GetUserDropDown();
    $batch = new ManageBatch();
    $batch_details=$batch->GetBatchById($id);

    return view('batch.edit',array('batch_details'=>$batch_details[0],'batch_type'=>$batch_type,'batch_user'=>$batch_user));


}

因为我将一行传递给视图。我必须添加索引[0]作为回报。最后它起作用了