没有在where子句中获取id - Laravel 5.2

时间:2016-02-29 20:38:37

标签: php laravel

我需要获取具有特殊ID的记录,我在我的方法中有这个:

public function addedMark()
{
    $user = Auth::user();
    $subject = ClassSubject::where('teacher_id', $user->id)->pluck('id','subject_id');

    return view('educator.account.marks', [
        'user' => $user,
        'marks' => StudentMark::where('subject_id', $subject)->get()
    ]);
}

当我dd(ClassSubject::where('teacher_id', $user->id)->pluck('id','subject_id'));时,我发现我得到了我需要的信息,但当我dd(StudentMark::where('subject_id', $subject)->get());时,它会返回一个空数组。

知道为什么吗?

2 个答案:

答案 0 :(得分:1)

$subject中,您有idsubject_id。你可能想要subject_id

所以改变一下:StudentMark::where('subject_id', $subject)->get()

StudentMark::where('subject_id', $subject[1])->get()

答案 1 :(得分:1)

将其更改为(whereIn)

 'marks' => StudentMark::whereIn('subject_id', $subject)->get()

让我们看看hapens