如何将参数传递给路线

时间:2019-04-17 14:25:43

标签: php laravel

所以每个“项目”都有这个

@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

我需要帮助的地方是<a class="dropdown-item" href="{{route('projects.tasks',$project)}}">tasks</a>

该路线到达我的控制器:

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

我需要将单击的项目的ID传递给该控制器,以便显示与其链接的所有任务。我已经完成所有关系,只需要这部分的帮助。 我不知道该怎么做。

这是我现在在控制器中拥有的功能:

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

}

它返回一个空数组[]

预先感谢

1 个答案:

答案 0 :(得分:2)

为此,您需要使用路由模型出价。所以您的路线定义将是

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

Laravel将自动获取您传递到路线的项目,如果模型不存在,则会引发异常。