Reputation: 17
I have a model that searches my DB. Works fine. I have a controller that gets the result of this model,and returns it in a route. Works fine too. But i want to make this Database Search dinamically so the user can change the conditions.For this,i tried using a variable in my model DB seach (working), send its parameter to controller (working), and now,all i need to do is pass the value from my input to this controller. But i have absolutely no idea on how to do that;
Here is my model (user);
public function dbtest($testValue)
{
return DB::select("SELECT * FROM users WHERE name >= '$testValue'");
}
My Controller:
public function returnJSON()
{
$testValue = 170;
$teste = new user();
return view('historyAcess',['resultadoJSON'=>$teste->dbtest($testeValue)]);
}
Route:
Route::get('returnJSON', 'App\Http\Controllers\Controller@returnJSON', ['name' => 'returnJSON']);
All i need to do in my blade is to get the value from the submit and send it to my controller '$testValue'. It can be via route or anything...
<form action="?????????" method="???????">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="submit" value="Submit">
Upvotes: 0
Views: 1512
Reputation: 2978
Okay, so, first of all, you have to close the form
tag
If you had to do a POST request - not your case - it would be necessary to add @csrf
right after the begin of the form tag, it is for security, you can learn more here, you can use it on GET methods, but it is not necessery
Then on action you have to place the address to your route you've created, you can do it manually /returnJSON
or, since you attached a name to it, you can use a blade helper {{ route('returnJSON') }}
, on method you have to place the same method you wrote on the route, in this case it is GET
, so it will be <form action="{{ route('returnJSON') }}" method="GET">
So on your controller, you're setting the value fixed as 170, I imagine that you want to get the value of the input fname
instead, so what you have to do is $testValue = request('fname');
you can debug and see exacly what are you receiving from the request with dd(request()->all())
than on your view historyAcess
you need to show the value that you just got
This is pretty much the basics, I recommend you watching the series Laravel 8 from scratch it is free, and will help you to undestand the basics of Laravel
Upvotes: 3