簡體   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