Home > OS >  How to display customer which have particular id?
How to display customer which have particular id?

Time:01-12

I want to display user which have particular id. For example for id 1 should display customer which have id 2. Now redirect me to customers/1 but show empty page. What can I do that display user ? route

Route::get('/', function () {
    return redirect()->route('customers.index');
});
Route::get('/customers', '\App\Http\Controllers\CustomerController@index')->name('customers.index');
Route::get('/{id}', '\App\Http\Controllers\CustomerController@show')->name('customers.show');

controller

public function show(Customer $customer)
{
    return view('show', compact('customer'));
}

view customers

a href=" {{ route('customers.show', ['id' => $customer->id]) }} ">

view customers/1

<tr>
<td>{{ $customer->first_name }}</td>
<td>{{ $customer->last_name }}</td>
<button type="submit" >Delete</button>
<button type="submit" >Updated</button>
</tr>

CodePudding user response:

Route: you can customize the route as you like, but the standard is as follows:

Route::get('/customers/{customer}', '\App\Http\Controllers\CustomerController@show')->name('customers.show');

CustomerController: when you do route model binding, it will do the findOrFail, so if you pass a customer that doesn't exist, you will get a 404 error automatically.

public function show(Customer $customer)
{
    return view('customers.show', compact('customer'));
}

customers.show view: basic view to show data compacted from the controller

<tr>
   <td>{{ $customer->first_name }}</td>
   <td>{{ $customer->last_name }}</td>
</tr>

Simple, yet elegant :)


You can read how laravel prefers to structure routes here: https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller

You can also go 1 step further, and let laravel do everything for you with the simple artisan command:

php artisan make:model Customer -mcr

Which will create, a resource, a migration, and a full resource controller to look perfect and ready to be modified for you :)

Then in your web.php route:

Route::resource('customers', 'CustomerController');

CodePudding user response:

Try change you code in routes file to:

Route::prefix('/customers')->group(function () {
    Route::get('/customers', '\App\Http\Controllers\CustomerController@index')->name('customers.index');
    Route::get('/{id}', '\App\Http\Controllers\CustomerController@show')->name('customers.show');
});
  • Related