Danielle Rose Mabunga
Danielle Rose Mabunga

Reputation: 1724

Form input as parameters Laravel Eloquent

I want to dynamically query using dynamic values from form inputs

My form looks like this

<form action="{{url('search')}} method="post">
 <input name="min" type="number">
 <input name="max" type="number">
 <button type="submit">Submit</button>

Routes

Route::post('search', 'SearchController@search');

Action

public function search($min, $max)

{
   $max = //this should from form
   $min= //this should from form
   $result = $this->users->showResultByAgeMinMax($max,$min);//some code for repository


}

How whould I pass data from form as values for min, max as parameters?

 $min = 15;//works
 $max = 100;//works//

But I want this dynamically populated from form by a user

Upvotes: 0

Views: 355

Answers (1)

Claudio King
Claudio King

Reputation: 1616

There are few methods to do that.

1. Request object (recommended).

public function search(Request $request)

{
   $max = $request->input("max");
   $min= $request->input("min");
   $result = $this->users->showResultByAgeMinMax($max,$min);//some code for repository


}

Or:

public function search(Request $request)

{
   $max = $request->get("max");
   $min= $request->get("min");
   $result = $this->users->showResultByAgeMinMax($max,$min);//some code for repository


}

2. Input facade.

public function search()

{
   $max = Input::get("max");
   $min= Input::get("min");
   $result = $this->users->showResultByAgeMinMax($max,$min);//some code for repository

}

3. PHP $_POST superglobal.

public function search()

{
   $max = $_POST["max"];
   $min= $_POST["min"];
   $result = $this->users->showResultByAgeMinMax($max,$min);//some code for repository

}

Upvotes: 1

Related Questions