duan0818 2018-06-09 10:45
浏览 84

使用modal存储数据并传递$ id变量

I want to pass the variable id from (the url is /xxxx/{{id}} ) to store

I have a modal box which opens a form. This info is then stored using the controller shown below:

I tried adding $id to pass through the controller (the url is /xxxx/{{id}} )

OpenHome Controller

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');

ERROR: Type error: Too few arguments to function App\Http\Controllers\option::store(), 1 passed and exactly 2 expected

dd($request->all());

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

1条回答 默认 最新

  • douguanyan9928 2018-06-09 13:59
    关注

    In your question you imply an update of an existing record, but in your code it implies the creation of a new option.

    If you wanted to update an option with the $id = 1 you need to have the following route.

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

    Inside your controller.

    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'); 
    }
    

    However if you just wanted to pass an additional variable to the store method for use inside the store method, make sure the Route only has single set of {} around the variable.

    Route::post('/option/{variable}', 'OptionController@store');
    
    public function store(Request $request, $variable)
    {
      // User the $variable passed as a route parameter.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)