-1

I'm building a filter functionality (which already works). Now I just want to add an order functionality to it: Order by "artist name", "artwork title",...

The possible order values aren't "fields" in this table. I can't find a way to start this "orderBy" query. What I'm looking for is something like this:

$artworks = $artworks->orderBy(function($q) use($request){
    $q->where('title', 'LIKE', '%artist_name%');
});

But obviously this doesn't work. Is there a way to do this? For example: I want to be able to sort on artworks that have a title "artist_name", ordered by the value of those records

I have two tables: Artworks & Artwork_data (one to many relationship).

This is my Artwork_data table:

Artwork_data table

RW24
  • 624
  • 2
  • 10
  • 19

1 Answers1

0

You could use orderByRaw with conditional clause

->orderByRaw('case when title like %artist_name% then value else somecolumn end asc')
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118