Ridiculous
Ridiculous

Reputation: 35

How to add few parameters to URL with form in Laravel

I have problem with forms in laravel.

web.php (routes)

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

And I want to add the form which will generate URL from inputs.

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:

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

... but does not work.

Upvotes: 1

Views: 97

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

You should use POST method to send form data:

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:

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.

Upvotes: 1

Related Questions