【问题标题】:Store data using modal and pass $id variable使用模态存储数据并传递 $id 变量
【发布时间】:2018-11-19 06:27:04
【问题描述】:

我想将变量 id 从(the url is /xxxx/{{id}} ) 传递到存储

我有一个打开表单的模式框。然后使用如下所示的控制器存储此信息:

我尝试添加$id 以通过控制器(the url is /xxxx/{{id}} )

OpenHome 控制器

public function store(Request $request,$id)

    $option = new Option;
    $option->time = $request->input('time');
    $option->date = $request->input('date');
    $option->save();
    return view('option.create');

错误:类型错误:函数参数太少 App\Http\Controllers\option::store(),1 个通过,2 个预期

dd($request->all());

array:3 [▼
  "_token" => "7O23EkldYOYj1d7Fc2xNflcPWMY67ytGTkrymy9g"
  "time" => "00:12"
  "date" => "12-05-1996"
]

【问题讨论】:

  • 你能显示你的路线吗?
  • 试试 dd($request->all());并显示输出
  • 您的方法现在需要两个参数,而您只使用一个参数来调用它。这就是错误消息的内容。
  • @SachinAghera 完成
  • @kerbholz 是的,我试图找到一种方法来传递 $id

标签: php sql laravel laravel-blade


【解决方案1】:

在您的问题中,您暗示更新现有记录,但在您的代码中,它暗示创建新选项。

如果您想使用$id = 1 更新选项,您需要有以下路线。

Route::put('/options/{id}', 'OptionController@update');

在您的控制器内部。

public function update(Request $request, $id)
{
  $option = Option::find($id);
  $option->update([
    'time' => $request->input('time'),
    'date' => $request->input('date'),
  ]);

  // Redirect to display the list of options.
  return redirect('/options'); 
}

但是,如果您只是想将一个附加变量传递给 store 方法以在 store 方法中使用,请确保 Route 在变量周围只有一组 {}

Route::post('/option/{variable}', 'OptionController@store');

public function store(Request $request, $variable)
{
  // User the $variable passed as a route parameter.
}

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 2015-02-20
    • 2013-05-28
    • 1970-01-01
    • 2019-01-16
    • 2020-12-02
    • 1970-01-01
    • 2015-09-24
    • 2020-07-07
    相关资源
    最近更新 更多