Skip to content
Advertisement

How to prioritize result in laravel eloquent

I have a table with 4 columns. I want to display the row based on input and it should be able to display with priority according to the priority fields.

example: My table looks like this:

I have my test cases below how it should work:

Now, I want to prioritize the result from title down to tags based in the input.

But the above codes, seems that it didn’t..

Advertisement

Answer

I think the logic you want to order your result should be appearing in an ORDER BY clause. Consider the following raw query:

Output:

Image showing ordered results

Here is my attempt at what the Laravel code would look like using orderByRaw:

I couldn’t find any (working) documentation on how to parameterize an orderByRaw, so I used string concatenation instead.

Demo here:

Rextester

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement