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

chandy's avatar

How to output data using query builder while eliminating duplicates

I have a table of results where there is adm_no, subject_id, exam_id and score fields. I inserted the data using the updateOrCreate method in laravel 5.4.

When I fetch the data I still get duplicates. e.g. when a user inserts marks for instance 2 students for a given subject and repeats it for the same students and subject I should get the latest row of all results.

Here is a sample of my query:

public function searchStudent() { $exam = Exam::all(); $term = Term::all(); $a = Input::get('adm_no'); $e = Input::get('exam_id'); $t = Input::get('term_id'); $y = Input::get('year'); $results = DB::table('results') ->select(DB::raw('DISTINCT(subject_id)')) ->where('adm_no', 'LIKE', '%' . $a . '%') ->where('exam_id','LIKE', '%' . $e . '%') ->where('term_id', 'LIKE', '%' . $t . '%') ->where('year', 'LIKE', '%' . $y . '%') ->orderBy('created_at', 'desc') ->get(); dd($results); }

0 likes
6 replies
cviv's avatar

How changing your query first

DB::table('results')->distinct('subject_id')
->where('adm_no', $a)
->where('exam_id',$e)
->where('term_id', $t)
->where('year', $y)
->orderBy('created_at', 'desc')
->get(); 

any change in the result?

1 like
chandy's avatar

@cviv . Not yet. this is the case. i have a results table. it has adm_no(admission no. for a student),exam_id,year,subject_id, and score. when i insert marks it adds a duplicate with updated score field so i would like to fetch the latest score for each student for each subject.

this is how i performed the insert:

}
public function pScores(Request $request){


    $adm_no=$request->input('adm_no');
    $year=$request->input('year');
    $term_id=$request->input('term_id');
    $exam_id=$request->input('exam_id');
    $subject_id=$request->input('subject_id');
    $score=$request->input('score');
    $total=count($adm_no);
    for($i=0; $i<$total; $i++){
        $data=[
            'adm_no'=>$adm_no[$i],
            'year'=>$year,
            'term_id'=>$term_id,
            'exam_id'=>$exam_id,
            'subject_id'=>$subject_id,
            'score'=>$score[$i]
        ];
        Result::create($data);
    }

    return redirect()->back()->with('success','Marks Submitted  successfully!');
} 

could this insert be the problem?

r17x's avatar

change this

Result::create($data);
//to
//If you don't want duplicate data
Result::firstOrCreate($data);
2 likes
chandy's avatar

Still not getting the result i want.

let's say the first record for adm_no: 1 , subject_id :1 and had a score of 35 and a second score for the same student and subject was 40 how can i pick 40 and ignore 35 in my query. this is what i have so far..

  public function searchStudent(){

$a= Input::get( 'adm_no' );
$e= Input::get( 'exam_id' );
$t= Input::get( 'term_id' );
$y= Input::get( 'year' );
$results=DB::table('results')->distinct('subject_id')
->where( 'adm_no', $a)
->where( 'exam_id', $e)
->where( 'term_id',$t)
->where( 'year',$y)
->groupBy('subject_id')
->latest()
->get();
dd($results);
}

but it is giving me the first record instead of the latest. the student can have many subjects. so i want to return the results with the latest timestamp incase they were updated. please help.

cviv's avatar

how about

  public function searchStudent(){

$a= Input::get( 'adm_no' );
$e= Input::get( 'exam_id' );
$t= Input::get( 'term_id' );
$y= Input::get( 'year' );
$results=DB::table('results')->distinct('subject_id')
->where( 'adm_no', $a)
->where( 'exam_id', $e)
->where( 'term_id',$t)
->where( 'year',$y)
->groupBy('subject_id')
->latest()
->first();
dd($results);
}
1 like
chandy's avatar
chandy
OP
Best Answer
Level 1

Thank you guys for giving me clues.i have combined your suggestions and i have got a solution which is.

    public function searchStudent(){

          $a= Input::get( 'adm_no' );
          $e= Input::get( 'exam_id' );
         $t= Input::get( 'term_id' );
          $y= Input::get( 'year' );
           $results=DB::table('results')->distinct('subject_id')
          ->where( 'adm_no', $a)
          ->where( 'exam_id', $e)
          ->where( 'term_id',$t)
          ->where( 'year',$y)
          ->groupBy('subject_id')
           ->latest('subject_id')
           ->get();
         dd($results);
            }

Please or to participate in this conversation.