1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.