laravel - Laravel:An entity has 2 foreign keys how to display that entity which belong to the specific table?
问题描述
I have an entity call submission, it has 2 foreign key users and task. A user has many submission, submission belongs to many users. A task has many submission, submission belongs to many tasks. In my controller
public function go_to_self_marking($id){
$task = Task::find($id);
//auth user
$user = auth()->user();
$task_criterias = $task->criterias;
$user_submissions = $user->submissions;
//user task criterias submission
return view('criterias/self-marking')
->with('task_criterias',$task_criterias)
->with('task',$task)
->with('user_submissions',$user_submissions);
}
In my view
<div>
@if($user_submissions!== null )
@foreach($user_submissions as $user_submission)
<object type="text/html" data="{{$user_submission->url}}"
width="400px" height="350px"
style="overflow:auto;border:5px ridge blue">
</object>
@endforeach
@else
<p>No submissions</p>
@endif
</div>
In this way I can show the all the submission, but it cannot achieve that submission belongs to the specified task. Each of my tasks displays all the submission which links to the user. How can I fix this?
I changed the code in my controller
public function go_to_self_marking($id,Submission $submission){
$task =Task::find($id);
//auth user
$user = auth()->user();
$task_criterias =$task->criterias;
$task_submissions =$task->submissions;
//user task criterias submission
return view('criterias/self-marking')->with('task_criterias',$task_criterias)
->with('task',$task)->with('task_submissions',$task_submissions)->with('submission',$submission);
}
In my view
{{--@if(Auth::user()->id == $submission->id)--}}
@if($task_submissions!== null )
@foreach($task_submissions as $task_submission)
<object type="text/html" data="{{$task_submission->url}}" width="400px" height="350px" style="overflow:auto;border:5px ridge blue">
</object>
@endforeach
@else
<p>No submissions</p>
@endif
{{--@endif--}}
Now the submission belongs to specified task, but every user can access the submission.
解决方案
我相信你需要的submissions
是属于特定user
的和特定的task
。
在您的控制器功能中。
public function go_to_self_marking($id)
{
$task = Task::find($id);
$user = auth()->user();
// these are the submissions you want.
$user_task_submissions = $task->submissions()
->where('user_id', $user->id)
->get();
...
...
// rest of the code goes here.
// send these $user_task_submissions to your view.
// and iterate over them in your view.
}
推荐阅读
- angular - 在 Angular 中隐藏部分菜单(权限)
- javascript - 使用 fabricjs 执行语义缩放
- filesystems - 安装不同版本的应用程序时出现签名错误
- angular - Angular 7 - 有没有办法存储 JSESSIONID 并在每个请求中发送它?
- docker - 在构建期间将参数传递给通过 Dockerfile 运行的脚本
- flutter - CachedNetworkImage 图像不会以相同的名称更改
- regex - 根据附件中的特定内容转发 Outlook 电子邮件
- excel - 如何使用 VBA 将计算列添加到 PowerPivot 数据模型中的表?
- vb.net - 使用子例程和函数进行错误处理的正确方法是什么?
- python-3.x - 使用库 pyttsx3 的对象没有属性错误