如何从我单击的任务中获取ID

时间:2019-04-18 16:20:34

标签: php laravel

所以我有“任务”,每个任务可以有多个注释。 我显示如下任务:

<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, 

        ]);


    }

预先感谢

1 个答案:

答案 0 :(得分:1)

发送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)