How to use alias column in whereIn with Laravel?

◇◆丶佛笑我妖孽 提交于 2020-08-07 07:47:51

问题


here is my code. I am trying to get new list of A items in order to loop on.

 $allowed_a = \App\NewA::select('name')->get()->pluck('name');        
 $a = App\A::selectRaw("replace(unaccent(trim(name)), ' ', '') AS newname, name")
             ->whereIn('newname', $allowed_a)->get();

But I am getting Undefined column 'newname'. How can I fix it please? thanks


回答1:


You should be able to achieve something equivalent using:

$allowed_a = \App\NewA;
\App\A::selectRaw('replace(unaccent(trim(name)) as newname')
      ->whereRaw('replace(unaccent(trim(name)) IN ("'.implode('","', $allowed_a).'")')
      ->get()


来源:https://stackoverflow.com/questions/62953660/how-to-use-alias-column-in-wherein-with-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!