Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

Merge two arrays into single arrays in php laravel and displaying in blade

I want to filter the datas according to colleges and universities and my query is

$exams = Exam::where('status', 1);
        if ($request->filled('universities')) {
            $exams = $exams->whereIn('university_id', $request->universities);  //data 1st
        }
        if ($request->filled('colleges')) {
            $exams = $exams->whereIn('college_id', $request->colleges); //data 2nd
        }
        $exams = $exams->get();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You would want to group the wheres for the college or university. You can try something like this:

$exams = Exam::where('status', 1);

if ($request->has('universities', 'colleges')) {
    $exams->where(function ($q) use ($request) {
        if ($request->input('universities')) {
            $q->orWhereIn('university_id', (array) $request->input('universities'));
        }

        if ($request->input('colleges')) {
            $q->orWhereIn('college_id', (array) $request->input('colleges'));
        }
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...