【问题标题】:Missing argument 2 for App\Http\Controllers\UserController::store()App\Http\Controllers\UserController::store() 缺少参数 2
【发布时间】:2016-12-11 10:36:52
【问题描述】:

控制器

  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 的新手。

【问题讨论】:

    标签: php laravel drop-down-menu laravel-5.3


    【解决方案1】:

    您不能通过这种方式将其他数据传递给store 操作。

    您可以使用php artisan route:list 命令查看这条路线。如您所见,它不期望也不传递任何数据。

    因此,您需要在隐藏输入中传递 ID:

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

    并使用$request-&gt;userId获取控制器中的数据

    不要忘记从store() 中删除$id 和从Form::open() 中删除$users-&gt;id

    另外,Form::open() 的正确语法(固定错别字)是:

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

    【讨论】:

    • 感谢您的帮助。但它仍然给了我Missing argument 2 for App\Http\Controllers\UserController::store()我认为错误在我的控制器@Alexey Mezenin 的某个地方
    • $new-&gt;status = $request -&gt;input('field');这行代码我没信心
    • 它返回正确的用户ID(即汽车ID)
    • 谢谢!!解决了通过id。选择的值怎么样,如何在请求中传递它的名称?
    • 这里也需要使用正确的语法。 {!! Form::select('select_list_name', ... 然后使用$request-&gt;select_list_name 获取控制器中的选定元素。您应该阅读docs 以熟悉Form:: 子句的正确语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2014-01-10
    • 2021-11-12
    • 2016-09-16
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多