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

redlik's avatar

DB::table query error

I'm trying to run a query with orderBy using DB::table('subjects')->orderBy('name', 'desc') but it throws an error Undefined property: Illuminate\Database\MySqlConnection::$id.

If I do query like this Subject::all() or any of these - it works fine.

I tried with use DB or use Illuminate\Support\Facades\DB but none of them work.

My model is Subject and table is called subjects

What am I doing wrong?

0 likes
3 replies
dezineHQ's avatar
dezineHQ
Best Answer
Level 30
Try

DB::table('subjects')->orderBy('name', 'desc')->get()

or 

Subject::orderBy('name','desc')->get()

redlik's avatar

I was missing ->get() at the end :-(((

Thnx

dezineHQ's avatar

you welcome. don't forget to mark as best answer :)

Please or to participate in this conversation.