Laravel - passsing variables to controller

131 Views Asked by At

I am passing 2 random numbers from controller to view with form:

public function request()
{       
    $number1 = rand(10,20);
    $number2 = rand(0,10);
    return View::make('request', ['num1' => $number1, 'num2' => $number2]);
}

These 2 numbers are displayed in form:

{{ Form::open(array('action' => 'MyController@verifyRequest', 'class'=>'bg-grey width')) }}
   {{ Form::label('Check sum: ') }}
   {{ Form::label($num1. ' + '. $num2. ' = ') }}
   {{ Form::text('checksum') }}
{{ Form::close() }} 

Now how can I pass those two variables into controller method 'verifyRequest' to check sum of the numbers?

public function verifyRequest()
{   
    $sum = ???
3

There are 3 best solutions below

6
On BEST ANSWER

You can either put those data into session (what might be better solution) or add hidden fields into form:

{{ Form::hidden('num1', $num1) }}
{{ Form::hidden('num2', $num2) }}

and now in your controller you can use:

$sum = Input::get('num1') + Input::get('num2');
0
On

You can insert num1 and num2 sum result at session and check in verifyRequest method.

0
On
  {{ Form::open(array('action' => 'MyController@verifyRequest', 'class'=>'bg-grey width')) }}
   {{ Form::label('Check sum: ') }}
   {{ Form::label($num1. ' + '. $num2. ' = ') }}
   {{ Form::text('checksum') }}
   {{ Form::hidden('num1', $num1) }}
   {{ Form::hidden('num2', $num2) }}
   {{Form::submit('Click Me!');}}
{{ Form::close() }} 

in controller

 public function verifyRequest()
 {   
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2']
    $checksum = $_POST['checksum']
    //Do whatever you want
 }