2016-12-11 31 views
2

控制器缺少參數2爲App HTTP 控制器 UserController中::店()

public function store(Request $request,$id) 
{ 
    $new = Car::find($id); 

    $new->status = $request ->input('field'); 
    $new->save(); 

    redirect('home'); 
} 

查看

@foreach($users as $user) 
       @foreach($user->cars as $users) 
       {!! Form::open(['route' => 'user.store', 'method'=>'post','id'=> '$users->id']) !!} 
       <th scope="row">1</th> 
       <td>{!! Form::label($cars->name)!!}<td> 
       <td>{!! Form::label($cars->age)!!}<td> 
       <td>{{Form::select($cars->status, ['R' => 'Requested', 'C' => 'Coming', ['name' => 'field']])}} // name is worong but I dont know the alternative 
       <td>{!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}</td> 
       {{ Form::close() }} 

路線

Route::Resource('user', 'UserController'); 

問題

嘗試將所選值從status保存到汽車模型中。但我得到一個有關參數的錯誤。有人能告訴我我的錯誤在哪裏嗎?我是Laravel的新手。

回答

1

您不能通過這種方式將其他數據傳遞給store

您可以使用php artisan route:list命令查看此路線。正如你所看到的,它不會期望也不會傳遞任何數據。

所以,你需要通過在隱藏輸入ID:在控制器

{!! Form::hidden('userId', $user->id) !!} 

而得到的數據與$request->userId

不要忘記從store()$users->idForm::open()

刪除$id另外,對於Form::open(),正確的語法(與固定的拼寫錯誤)是:

{!! Form::open(['method' => 'post', 'route' => 'user.store']) !!} 
+0

謝謝你的幫助。但它仍然給我'應用程序\ Http \ Controllers \ UserController :: store()缺少參數2'我認爲錯誤在我的控制器@Alexey Mezenin – leo0019

+0

'$ new-> status = $ request - > input('字段');'我對這行代碼沒有信心 – leo0019

+0

它返回正確的用戶ID(這是汽車ID) – leo0019