the route shoule be Route::post('/customer/show/{name} ...
it's supposed to be like function($name){...} somewhere in there too, huh?
How do I pass the name from the routes.php to the controller?
Route::post('/customer/show/{name}', [
'uses' => 'CustomerController@showOrRegisterCustomer',
'as' => 'showOrRegisterCustomer',
'name' => $name
]);
First of all you have a typo in your routes.php file
Route::post('/customer/show{name}', [
a slash(/) is missing after show.
Second of all, I see that you already imported the model class so you should be fine with
Customer::where('name', $name)->get();
muppbarn said:
How do I pass the name from the routes.php to the controller?
Route::post('/customer/show/{name}', [ 'uses' => 'CustomerController@showOrRegisterCustomer', 'as' => 'showOrRegisterCustomer', 'name' => $name ]);
You have already done it here
class CustomerController extends Controller
{
public function showOrRegisterCustomer($name)
{
// here you can return $name
}
}
szkolaweb, Maybe you're ahead of me here...
If I type this in the controller I get an error. I think it might be because the controller doesn't receive the $name
public function showOrRegisterCustomer($name)
{
echo $name;
}
Then if the customer name does not exist I wanna show a form, and in that case I don't need to go to the controller and back, yea? you're implying that I can call the model directly from the routes.php in that case?
So your route should look like this:
Route::post('customer/show', [
'uses' => 'CustomerController@showOrRegisterCustomer',
'as' => 'showOrRegisterCustomer'
]);
As you can see, I didn't have to pass any parameters. To catch the value from your form, you need to set a Request object, which contains data from the form, as a parameter in your method.
public function showOrRegisterCustomer(Request $request)
{
// to return a value you do this:
// $request->(name of the field from your form)
// Example: $request->name;
}
Yea, thanks. That post is actually a typo but with your guidance I found that
Route::get('customer/show/{name}', [
'uses' => 'CustomerController@showOrRegisterCustomer',
'as' => 'showOrRegisterCustomer'
]);
And then we need a model which we create from the terminal: php artisan make:model Customers
Then in the CustomerController we need to namespace in the model
//...
use App/Customer;
public function showOrRegisterCustomer()
{
$Customer = Customer::where('name', '=', $name);
}
Why are you not providing any error messages. I can only assume what's happening. Now, what you are doing wrong is you are not catching the name parameter you are sending through the url and since you are using the where method you have to get the data with the get method. So it should look like this:
public function showOrRegisterCustomer($name)
{
$Customer = Customer::where('name', '=', $name)->get();
Yea that was another typo. Somehow ...->get() didn't work out though, but if I change that to ->first() it works, what does ->get usually do?
My fault this time. You would use get if you want retreive an whole collection of things. For example you have a online shop and want display all products, you use get method. The method first returns this first item in your collection, which in this case is logical, because you are searching for one specific user.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community