简体   繁体   English

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

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

I have problem with forms in laravel.我在 Laravel 中遇到表单问题。

web.php (routes) web.php(路由)

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

TestController.php测试控制器.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);
    }
}

And I want to add the form which will generate URL from inputs.我想添加将从输入生成 URL 的表单。

Something like that:类似的东西:

{!! 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() !!}

This should generate URL like this:这应该生成这样的 URL:

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

... but does not work. ...但不起作用。

You should use POST method to send form data:您应该使用POST方法发送表单数据:

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

Then use correct route name and remove parameters:然后使用正确的路由名称并删除参数:

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

Then get data in the controller using Request object:然后使用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);
}

When you use resource routes and controllers , you should use POST or PUT methods to send the form data.当您使用 资源路由和控制器时,您应该使用POSTPUT方法来发送表单数据。

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

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