【问题标题】:How to add few parameters to URL with form in Laravel如何在 Laravel 中使用表单向 URL 添加一些参数
【发布时间】:2017-01-11 18:21:34
【问题描述】:

我对 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

...但不起作用。

【问题讨论】:

    标签: php mysql forms laravel


    【解决方案1】:

    您应该使用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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 2012-05-01
      相关资源
      最近更新 更多