首页 > 解决方案 > 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.

标签: laraveleloquentforeign-keysrelational-database

解决方案


我相信你需要的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.
}

推荐阅读