繁体   English   中英

如何在 Laravel 中使用表单向 URL 添加几个参数

[英]How to add few parameters to URL with form in Laravel

我在 Laravel 中遇到表单问题。

web.php(路由)

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

测试控制器.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对象在控制器中获取数据:

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

当您使用 资源路由和控制器时,您应该使用POSTPUT方法来发送表单数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM