【问题标题】:Missing required parameters for in a Update Form Laravel 5.2在更新表单 Laravel 5.2 中缺少必需的参数
【发布时间】:2017-04-09 18:10:37
【问题描述】:

我最近一直在 laravel 中开发一个 webapp,我想在应用程序中拥有一个 eddit 功能。但我收到此错误缺少 [Route: producten.update] [URI: producten/{producten}] 所需的参数,我不知道我做错了什么。

这是我正在使用的路线:

Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'update', 'delete', 'edit', 'destroy', 'create']]);

这是我用来显示编辑页面和更新的控制器功能。

编辑功能

public function edit(Request $request, Product $product)
{
   // $product = Product::FindorFail($id);
    // Product is a table with all products, with sellprice and buy price
    // fabriek = table that has a foreign key attached to the product table
    return view('producten.edit', [
        'model' => $product,
        'fabrieks' => Fabriek::lists('Id')
        ]);

}

更新函数:

public function update(Request $request, Product $product)
    {

        $product->update($request->all());

        return redirect(Route('producten.index'));

    }

这是我使用的视图。

    {{Form::model($model, ['method' => 'PATCH', 'action' => 'ProductenController@update', $model ]) }}

    {{ Form::label('naam:')}}
    {{ Form::text('naam') }} <br>

    {{ Form::label('inkoopPrijs:')}}
    {{ Form::text('inkoopPrijs') }} <br>

    {{ Form::label('verkoopPrijs:') }}
    {{ Form::text('verkoopPrijs') }} <br>

    {{Form::label('Fabrieken', 'Fabrieken Id:') }}
    {{ Form::select('Fabrieken_Id', $fabrieks)}} <br>
    {{ Form::submit('edit')}}
    {{ Form::close() }}

如果还有什么我需要添加到问题中的,请告诉我,我会添加它

【问题讨论】:

  • 你的函数体中有一个参数,“Request $request, Product $product” 它将从哪里获取?它是如何通过您定义的路线的?

标签: laravel laravel-5.2


【解决方案1】:

缺少的是您在编辑功能中没有获得 id 的 id 您的编辑功能应该像我假设的那样,您只是通过这种方法显示用户可以编辑的表单

    public function edit($id)
{
       $product = Product::FindorFail($id);
       //Product is a table with all products, with sellprice and buy price
       //fabriek = table that has a foreign key attached to the product table
       return view('producten.edit', [
        'model' => $product,
        'fabrieks' => Fabriek::lists('Id')
        ]);

}

你的更新方法应该是这样的

public function update(Request $request, $id)
{

    $product->update($request->all());

    return redirect(Route('producten.index'));

}

你的路线应该是这样的,不需要只是

Route::resource('/producten', 'productionController');

编辑路线将是

<a href="{{ route('production.edit', $model->id) }}">

试试这个希望它会有所帮助

【讨论】:

  • 我已将 'action' => 'ProductenController@update' 更改为 'Route' => 'Producten.update', $model->Id,并将 $id 添加到函数中,并且它似乎有效,但现在当我按下编辑按钮时出现 MethodNotAllowedHttpException 错误,你知道如何解决它吗?
  • 您不需要为更新编写路由,只需为编辑编写,之后您的数据将自动传递到更新,您可以在其中更新为&lt;a href="{{ route('production.edit', $model-&gt;id) }}"&gt;
  • 有没有办法在更新路由 laravel 中不传递记录 id 来更新表单?我不想要这样的/producten/1/edit@ramzan-mahmood
  • @FaridVatani 是的,您可以发送带有隐藏类型的 id,将其放入控制器并做您想做的事情
  • 你能举个例子吗? @ramzan-mahmood
猜你喜欢
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2021-07-20
  • 2017-12-27
  • 1970-01-01
  • 2021-11-15
相关资源
最近更新 更多