dsf6281 2015-07-03 13:36
浏览 29

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.

  • 写回答

1条回答 默认 最新

  • dongyi1441 2015-07-03 13:46
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料