jeudi 12 septembre 2019

How to use index method to display a listing of the resource in Laravel

I created a customerController with Laravel resource. Now I want to show the list of the customers from the database using the index() method and it doesn't work when I visit the customer/index route. However if use the show() method instead and visit the customer/show route, it works perfectly.

Why does this behave this way and how do I get the index() method to do this?

class CustomerController extends Controller
{
    public function index()
    {
        $customers = Customer::all();
        return view('customer')->with('customers' , $customers);
    }

   public function show($id)
    {
       // adding the code in the index() method here makes the code run
       // as expected
    }
}

customer.blade.php

<ul>
    @foreach($customers as $customer)
        <li></li>
    @endforeach
</ul>

routes/web.php

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

I expect the output to be:

.sucre
.hameed
.micheal



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2UQHwEe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire