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

daniel21gt's avatar

Problems with query translation sql to eloquent, laravel 5.5

I try to translate this query

select governor_candidate from governor order by governor.id=3 desc;

not work

governor::orderBy ('id=3', 'DESC');

What I'm I missing of?

0 likes
7 replies
jlrdw's avatar

Order by ID not ID equals 3.

daniel21gt's avatar

I understand, but what can I do, if I need the id field to be able to change a value?

jlrdw's avatar
$somevar=governor::find (3);

Please read the documentation there are many examples.

daniel21gt's avatar

Yes, but a value appears, I explain, in the table I have 4 records, what I'm looking for is that when I change the id I show that as the first value, and then the others.

jlrdw's avatar

Not sure what you need, use this example from docs:

$somevar =governor::where ('id', '3') // replace 3 with a variable
               ->orderBy('some_field', 'desc')
               ->take(10)
               ->get();

But I don't understand, isn't id a unique field.

You probably need to take some of the free tutorials.

daniel21gt's avatar

It is a select, when I return the form I want it to show me the values that are already saved by each user, and not that the same select me for all, now, yes, I must see many tutorials, I still do not have an experience like yours

daniel21gt's avatar
daniel21gt
OP
Best Answer
Level 1

Thank very muchs jlrdw, this is the solutions

governor::select('governor_candidate') ->orderByRaw("FIELD(id, 3) DESC") ->get();

Please or to participate in this conversation.