I want to set up a route that fetches all Contact
s associated with a Customer
. The endpoint would look like /customers/{customer}/contacts
such that {customer}
provides context for which contacts should be returned. My routes/web.php
looks like this:
Route::get('/customers/{customer}/contacts', 'CustomerContactsController@index');
I generated the controller with --model=Contact
specified. As a result, several of the methods come with type hinting out of the box, but the index()
method doesn't, and actually won't run at all with any parameters supplied.
From CustomerContactsController.php
:
public function index(Customer $customer)
{
$this->authorize('update', $customer->account);
$contacts = $customer->contacts;
return view('contacts.index', compact('customer', 'contacts'));
}
This returns a completely blank screen. The associated Blade view looks like this:
@section('title')
{{ $customer->name }}
@endsection
@section('action')
<a href="{{ $customer->path() . '/contacts/create' }}" class="button" >{{ __('Add New Contact') }}</a>
@endsection
@section('content')
<ul>
@foreach ($contacts as $contact)
<li>{{ $contact->name }}</li>
@endforeach
</ul>
@endsection
I can still access the customer id inside the controller logic using Route::current()->parameters['customer']
, but isn't there a better/easier way to do this?