Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

masumluf's avatar

Laravel Having clause issue.

This is my code below , I am trying to get sum all value where mark should be grater than $passMark(50) with specific username. Like username(jack_89) has 5 subjects and he gets 48 (failed mark) in one subject. I need if any student failed any or more than one subject count will not be happened.

 $data =  Exam::with('student')
            ->select('student_username', 'class', 'section', DB::raw('sum(mark) as total'), 'mark')
            ->where('section', $request->section)
            ->where('class', $request->sclass)
            ->where('exam_type', $request->exam_type)
            ->whereBetween('created_at', [
                Carbon::now()->startOfYear(),
                Carbon::now()->endOfYear(),
            ])
            ->groupBy('student_username', 'class')
            ->orderBy('total', 'desc')
            ->having('mark', '>', '$passMark')
            ->get();
        return $data;

its always count all marks even student gets failed marks below 50 , thank you

0 likes
35 replies
jlrdw's avatar

Add in a > 48 in a where. If you don't want to include failed.

jlrdw's avatar

Try first, put order by last, having follows a groupby.

jlrdw's avatar

You reversed these two lines:

            ->orderBy('total', 'desc')
            ->having('mark', '>', '$passMark')
masumluf's avatar

@jlrdw after your suggestion I did my code as like as

$data =  Exam::with('student')
            ->select('student_username', 'class', 'section', DB::raw('sum(mark) as total'), 'mark')
            ->where('section', $request->section)
            ->where('class', $request->sclass)
            ->where('exam_type', $request->exam_type)
            ->whereBetween('created_at', [
                Carbon::now()->startOfYear(),
                Carbon::now()->endOfYear(),
            ])
            ->groupBy('student_username', 'class')           
            ->having('mark', '>', '$passMark')
            ->orderBy('total', 'desc')
            ->get();
        return $data;
Nakov's avatar

@masumluf Why is the variable in single quotes?

->having('mark', '>', $passMark)

this maybe?

jlrdw's avatar

I think this:

->having('mark', '>', '$passMark')

Needs to be a where clause under the select.

jlrdw's avatar

Wow @nakov I missed those quotes. Still a having follows a group by. Above was just a copy and paste.

Nakov's avatar

@masumluf does the variable exists at all? Or just add a number like 50 or whatever the case, otherwise you are comparing it to the string $passMark

jlrdw's avatar

Change to

            ->groupBy('student_username', 'class')           
            ->having('mark', '>', 48)
            ->orderBy('total', 'desc')
            ->get();

And tell us what happens

And what error

masumluf's avatar

@jlrdw still same result, @nakov yes $passMark is exist from appserviceprovider (view()->share('passMark', 50);)

Nakov's avatar

it exists in the view, not in the controller :)

jlrdw's avatar

You did not mention what error.

masumluf's avatar

@jlrdw no error i can see the result is counting all number even the student failed (got mark below 50)

masumluf's avatar

@nakov but it works somehow :O , but i put 50 instead of the variable same result. my query is not skipping failed student, it still counting all result even anybody failed .

jlrdw's avatar

Try deleting

->having('mark', '>', 48)

And put this in the above part:

->where('section', $request->section)
            ->where('class', $request->sclass)
            ->where('exam_type', $request->exam_type)
            ->where('mark', '>', 48)

Clear browser cache between changes.

masumluf's avatar

@jlrdw it is not counting the failed mark, but i can see the student still in the passed student list. :) If student failed any course or more than one course he should not be in count at all. he will be skip. becuase i need only all subject passed student list with sum total number.

jlrdw's avatar

change sum to count, by using sum, you are getting greater than 48 all the time.

masumluf's avatar

@jlrdw same, only one subject which is below 50 marks skipping and if i use count i cant see total marks, it shows only number of mark .

jlrdw's avatar

You have to use something else as count, like

DB::raw('count(mark) as mycount'), 'mark')

Just play around with what you call things in the query.

Aren't you after a count?

Also do another query, just looking for a failed mark, wouldn't that be easier.

masumluf's avatar

Yeah I did samething but no changes .

masumluf's avatar

Can I use this query inside eloquent ?

GROUP BY
    student_username, 
    (CASE WHEN mark> 50 THEN 0 ELSE markEND)
jlrdw's avatar

Okay you need sum, I wasn't sure which.

jlrdw's avatar

Remember these types of queries can take some trial and error.

Next

Please or to participate in this conversation.