gpt4 book ai didi

php - 如何在 Laravel 中使用表单向 URL 添加一些参数

转载 作者:行者123 更新时间:2023-11-28 23:20:59 24 4
gpt4 key购买 nike

我对 laravel 中的表单有疑问。

web.php(路由)

Route::get('/test/{param1}/{param2}', [
'as' => 'test', 'uses' => 'TestController@test'
]);

TestController.php

class TestController extends Controller
{
public function test($param1, $param2)
{
$key = Test::take(100)
->where('offer_min', '<=', $param1)
->where('offer_max', '>=', $param1)
->where('period_min', '<=', $param2)
->where('period_max', '>=', $param2)
->get();
return view('test.index')->with('key', $key);
}
}

我想添加将从输入生成 URL 的表单。

类似的东西:

{!! Form::open(array('route' => array('calculator', $_GET['param1'], $_GET['param2']), 'method' => 'get')) !!}
<input type="number" name="param1" value="Something">
<input type="number" name="param2" value="Something else">
<input type="submit" value="OK">
{!! Form::close() !!}

这应该生成这样的 URL:

http://your.site/test/123/1234

...但不起作用。

最佳答案

您应该使用POST 方法发送表单数据:

Route::post('/test', ['as' => 'test', 'uses' => 'TestController@test']);

然后使用正确的路由名称并删除参数:

{!! Form::open(['route' => 'test']) !!}

然后使用Request对象在controller中获取数据:

public function test(Request $request)
{
$key = Test::take(100)
->where('offer_min', '<=', $request->param1)
->where('offer_max', '>=', $request->param1)
->where('period_min', '<=', $request->param2)
->where('period_max', '>=', $request->param2)
->get();

return view('test.index')->with('key', $key);
}

当您使用 resource routes and controllers 时,您应该使用POSTPUT 方法来发送表单数据。

关于php - 如何在 Laravel 中使用表单向 URL 添加一些参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41598000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com