2017-01-11 20 views
1

laravelのフォームに問題があります。LaravelのフォームでURLにいくつかのパラメータを追加する方法

web.php(ルート)

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

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 

を...しかし、動作しません。

答えて

1

あなたは、フォームデータを送信するためにPOSTメソッドを使用する必要があります。

Requestオブジェクトを使用して、コントローラ内のデータを取得後

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

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

はその後、正しいルート名を使用してパラメータを削除します

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

フォームデータを送信するにはPOSTまたはPUTメソッドを使用する必要があります。

関連する問題