2016-03-29 44 views
0

我想用這種形式來更新我的數據,但我得到了一個錯誤:錯誤laravel從形式更新數據5.2

The webform 我的控制器:

public function update($id) 
{ 
    $dosenUpdate = Request::all(); 
    $dosen = Dosen::find($id); 
    $dosen->update($dosenUpdate); 
    return redirect('dosen')->with('message', 'Data berhasil dirubah!'); 
} 

public function status() 
{ 
    $dosen = \App\Dosen::paginate(5); 
    return view('dosen.status', compact('dosen')); 
} 

我的路線:

Route::get('/dosen/status', '[email protected]'); 

我的觀點:

{!! Form::model($dosen, ['route' => ['dosen.update', $dosen->id] !!} 
{!! Form::hidden('_method', 'PUT') !!} 
{!! Form::select('status', array('1' => 'Ready', '0' => 'Not Ready'), null, ['placeholder' => 'Pilih Status'], ['class' => 'form-control'], ['placeholder' => 'Pilih Status']) !!} 
{{ Form::button('<i class="fa fa-check-square-o"></i> Save', ['type' => 'submit', 'class' => 'btn btn-primary']) }} 
{!! Form::close() !!} 

錯誤響應:

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View:  
D:\XAMPP\htdocs\infodosen\resources\views\dosen\status.blade.php) 

我該如何解決這個問題?

+0

是my_view是status_blade.php? – Drudge

回答

1

你是triyng從一個對象集合(LengthAwarePaginator)獲取屬性。

要在您的視圖中獲取模型Dosen的id,您必須迭代該集合。

事情是這樣的:

@foreach($dosen as $d) 
    {!! Form::model($d, ['route' => ['dosen.update', $d->id] !!} 
    {!! Form::hidden('_method', 'PUT') !!} 
    {!! Form::select('status', array('1' => 'Ready', '0' => 'Not Ready'), null, ['placeholder' => 'Pilih Status'], ['class' => 'form-control'], ['placeholder' => 'Pilih Status']) !!} 
    {{ Form::button('<i class="fa fa-check-square-o"></i> Save', ['type' => 'submit', 'class' => 'btn btn-primary']) }} 
    {!! Form::close() !!} 
@endforeach 

如果你得到一個錯誤TokenMismatch,一定要包括路由組稱爲「網絡」中間件內部的路線。

例如:

Route::group(['middleware' => ['web']], function() { 
//put your routes here 
} 

這將需要在您的$錯誤變量沒有設置你的觀點裏面的錯誤的護理也

+0

它的工作。但是當我點擊保存時出現錯誤: TokenMismatchException在VerifyCsrfToken.php行67 @RDev – Ezra

+0

是一個ajax調用保存? – RDev

+0

沒有。我嘗試添加@if($ errors-> any())

    @foreach($ errors-> all()as $ error)
  • {{$ error}}
  • @endforeach
@endif' 我得到了錯誤:未定義的變量:錯誤 @RDev – Ezra