php - 如何从我单击的任务中获取 id
问题描述
所以我有“任务”,每个任务可以有多个笔记。我显示这样的任务:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Task Id</th>
<th>Project</th>
<th>Task title</th>
<th>Description</th>
<th>Status</th>
<th>Priority</th>
<th>Created by</th>
<th>Created on</th>
@if (Auth::user()->role=='admin')
<th>Admin</th>
@endif
</tr>
</thead>
<tbody class="">
@foreach ($task as $task)
<tr>
<td>{{$task->task_id}}</td>
<td>{{$task->project->proj_title}}</td>
<td>{{$task->task_title}}</td>
<td>{{$task->task_desc}}</td>
<td>{{$task->status}}</td>
<td>{{$task->priority}}</td>
<td>{{$task->user->name}}</td>
<td>{{$task->created_at}}</td>
<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('tasks.notes',$task)}}">Notes</a>
每个任务都是一行,正如您在最后看到的,有一个按钮可以让用户进入注释视图。我需要从您单击的任务中获取 task_id,因此我可以将该 task_id 分配给便笺,这样每个任务都有自己的便笺。这是我在“任务”和“笔记”之间的关系;任务模型:
public function notes(){
return $this->hasMany('App\Note','task_id');
}
备注型号:
public function task(){
return $this->belongsTo('App\Task','task_id');
}
这就是我显示笔记的地方:
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Note</th>
</tr>
</thead>
<tbody>
@foreach($notes->where('task_id',$task->task_id) as $note)
<tr>
<td>Created by {{$note->user}}<td>
<td>{{$note->note}}</td>
</tr>
@endforeach
</tbody>
</table>
我的 NoteController 索引函数:
public function index(Task $task)
{
$task_id = $task['task_id'];
return view('notes.index', [
'notes' => Note::all(),
'user' => User::all(),
'task' => $task_id,
]);
}
提前致谢
解决方案
在刀片中发送id
而不是整个对象,例如:
<a class="dropdown-item" href="{{route('tasks.notes', $task->id)}}">Notes</a>
然后在index
操作中接收它并获取相关的任务注释,例如:
public function index($task_id)
{
$task = Task::find($task_id);
return view('notes.index', [
'notes' => Note::all(),
'user' => $task->notes,
'task' => $task,
]);
}
在笔记刀片中,您只需遍历它们:
@foreach($notes as $note)
推荐阅读
- django - 了解 django 模板运算符“管道”
- postgresql - 与列大小的总和相比,表关系大小非常大
- javascript - 如何在反应中使用外部对象?
- msbuild - 带有仅 MSBuild 项目的 GitVersionTask v4
- java - 无法在 CentOS 7 上启动 zookeeper
- c# - 绑定动态输入
- android - AWS Lambda:errorMessage进程在完成请求之前退出
- asp.net-web-api - asp.net webapi 服务器和 angular2 应用程序之间的通信面临的错误
- javascript - 保存鼠标位置,然后有第二个更新的值
- c - 如何使用冒泡排序对二维数组进行排序?