首页 > 解决方案 > 如何从我单击的任务中获取 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, 

        ]);


    }

提前致谢

标签: phplaravel

解决方案


在刀片中发送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)

推荐阅读