Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent
Last updated 1 year ago.
0

the route shoule be Route::post('/customer/show/{name} ...

it's supposed to be like function($name){...} somewhere in there too, huh?

0

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
]);
0

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();
Last updated 8 years ago.
0

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
    }

}
0

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?

0

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

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);
}
Last updated 8 years ago.
0

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();
0

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?

0

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.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

muppbarn muppbarn Joined 29 Mar 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.