laravel通过多列的雄辩顺序并在一列中显示

I have 4 tables:

  • user
  • particular
  • pro
  • advertiser

I have a table where i show the users data. in this table i have a column named Name where i use the code below

                       <td>
                            @if($user->pro))
                                {{$user->pro->company_name}}
                            @elseif($user->particular))
                                {{$user->particular->name." ".$user->particular->last_name}}
                            @elseif($user->advertiser)
                                {{$user->advertiser->company_name}}
                            @endif
                        </td>

So this column is popuplated from 3 different tables and different column. How can i sort this column table.

You're going to need a complex ORDER BY clause that you won't be able to set up using Eloquent's methods. However, Eloquent builder offers orderByRaw() method that let's you use raw SQL expression in your ORDER BY clause. Combine that with a couple of SQL IF's (for MySQL see https://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_if) and you should get what you need.

In your case it would be sth like:

->orderByRaw("IF(user.is_pro,pro.company_name,IF(user.is_particular, particular.name, IF(user.is_advertiser,advertiser.company_name,'')))")

Keep in mind that ordering by such expressions might have negative performance impact and that using raw SQL might make your code less portable.