Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Query -> FIRST();

Tags:

sql

php

laravel

I am struggling with a query! In my SQL Server I put first(); instead of get(); and I get the last result so in LARAVEL 4.2 I get an error. Do you have any advice? Is there a different way to do this? I will give you the code in case you need more information. Thank you for your time!

$users = DB::table('Home_Students')
            ->select('home_firstname','home_lastname','LogSt_data','LogSt_date')
            ->join('Home_LogStudents','Home_LogStudents.LogSt_studid','=','Home_Students.home_id')
            ->join('LessonUnitSections','LessonUnitSections.leuns_ID','=','Home_LogStudents.LogSt_sectionID')
            ->join('LessonUnits','LessonUnits.leun_ID','=','LessonUnitSections.leuns_LessonUnitID')
            ->where('Home_LogStudents.LogSt_action','=',225)
            ->where('Home_LogStudents.LogSt_data','<>',0)
            ->where('Home_LogStudents.LogSt_sectionID','=',$id)
            ->orderBy('LogSt_date','home_firstname')
            ->get();
like image 208
JP.dev Avatar asked Oct 30 '25 13:10

JP.dev


1 Answers

Try to add orderBy desc to the query, that should solve your problem.

For example, instead of:

->orderBy('LogSt_date','home_firstname')

Use this:

->orderBy('LogSt_date', 'desc')->orderBy('home_firstname', 'desc')

Also, if you do not get error message using get(), try to use ->take(1)->get() instead of ->first()

like image 137
Alexey Mezenin Avatar answered Nov 02 '25 05:11

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!