Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SarahS's avatar
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.

0 likes
6 replies
gzai's avatar

you have set /animals/{{ $animal->id }}, then /animalphotos/create/{{ $animal->id }}

Snapey's avatar
Snapey
Best Answer
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
SarahS's avatar
Level 12

@snapey I'm a little bit confused as to what should be plural and what shouldn't.

I have this so far:

AnimalPhotoController.php

    public function create(Animal $animal) 
    {
        return view('animalPhotos.create', compact('animal'));
    }

web/routes.php

 Route::get('/animals/{animal}/photos/create', 'AnimalPhotoController@create')->name('animals.photos.create');

animals/index.blade.php

<td><a href="{{ route('animals.photos.create'), $animal }}">Add Photos</a></td>

When I click on that link I get:

Missing required parameters for [Route: animals.photos.create] [URI: animals/{animal}/photos/create]. (View: /Users/Sarah/Projects/catalog/resources/views/animals/index.blade.php)
MichalOravec's avatar

You have mistake here

<td><a href="{{ route('animals.photos.create', $animal)  }}">Add Photos</a></td>
Snapey's avatar

I'm a little bit confused as to what should be plural and what shouldn't.

Route::get('/animals/{animal}/photos/create'

Yes, you are correct, by convention, resource routes should be plural (the animals part). photo or photos is the same. Here I would be guided by the functionality. Are we creating one photo or many photos.

->name('animals.photos.create');

name is just a label. Call it what you like as long as you are consistent

Michal is correct, you just have the bracket in the wrong place in the route helper.

SarahS's avatar
Level 12

Thank you to you both for helping me out.

Please or to participate in this conversation.