0

So I have this for each "project"

@foreach ($projects as $project) <tr> <td>{{$project->proj_id}}</td> <td>{{$project->proj_title}}</td> <td>{{$project->proj_desc}}</td> <td>{{$client->find($project->client_id)->client_name}}</td> <td>{{$project->user->name}}</td> <td>{{$project->created_at}}</td> @if (Auth::user()->role=='admin') <td> <div class="dropdown"> <button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action</button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <a class="dropdown-item" href="{{route('projects.edit',$project)}}">Edit</a> <form method="POST" action="{{route('projects.destroy',$project)}}" onsubmit="return confirm('Are you sure you want to delete this?')"> @method('DELETE') @csrf <button class="dropdown-item" type="submit">Delete</button> @endif ------------> <a class="dropdown-item" href="{{route('projects.tasks',$project)}}">tasks</a> </form> </div> </div> </td> </tr> @endforeach 

Where I need help is in the line <a class="dropdown-item" href="{{route('projects.tasks',$project)}}">tasks</a>

That route goes to my controller:

Route::get('/projects/tasks', ['as' => 'projects.tasks', 'uses' => 'ProjectController@seeTasks']); 

I need to pass that controller the id of the project in which I clicked so I can display all the tasks linked to it. I already have all relations done I just need help with this part. I don't know what I need to do to accomplish this.

This is the function I have for now in my controller:

public function seeTasks(Project $project){ return $project; } 

It returns an empty array []

Thanks in advance

1

1 Answer 1

2

In order to accomplish this, you need to use route model biding. So your route definition will be

Route::get('/projects/{project}/tasks', ['as' => 'projects.tasks', 'uses' => 'ProjectController@seeTasks']); 

Laravel will automatically fetch the project you pass to the route, or raise an exception if the model does not exist.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, this was my solution.
Please consider marking it as the answer to your question.
To complete what Francinaldo Almeida said, here's the documentation for Laravel Route Parameters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.