you have set /animals/{{ $animal->id }}, then /animalphotos/create/{{ $animal->id }}
Apr 30, 2020
6
Level 12
Create link with id passed to it
I have a table with a list of animals on and I want to have a button to add photos:
@foreach($animals as $animal)
<tr>
<td><a href="/animals/{{ $animal->id }}">{{$animal->name}}</a></td>
<td>{{ $animal->sex === 1 ? "Female" : "Male" }}</td>
<td>{{$animal->dob}}</td>
<td>{{$animal->coatcolour}}</td>
<td><a href="/animalphotos/create">Add Photos</a></td>
</tr>
@endforeach
I've set up an AnimalPhotoController but I need to be able to pass the Animal ID when you click on Add Photos in the table above. How do I do that? Thanks.
Level 122
You would have a route like;
Route::get('/animals/{animal}/photo/create', 'AnimalPhotocontroller@create')->name('animal.photo.create');
then in your table, use the route helper to insert the animal at the correct location in the url
<a href="{{ route('animal.photos.create', $animal }}">Add Photos</a>
by passing in the animal into the route helper. Laravel will know to insert the route key (usually the id) into the route.
In your controller, you can accept the animal that wants a photo
//AnimalPhotoController
public function create(Animal $animal)
{
// here $animal should be the model
Please or to participate in this conversation.