Laravel雄辩选择CASE? [英] Laravel Eloquent Select CASE?
本文介绍了Laravel雄辩选择CASE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SELECT shares.id,...,
CASE WHEN用户。 id =< CurrentUser> THEN 1 ELSE 0 END AS is_user,
...
FROM< table>
...
源代码如下:
$ shares = Share :: where('shares.status','=',SHARE_STATUS_APPROVED)
- >其中('shares.deleted' ,'=','0')
- > where('locations.lat','< =',$ nelat)
- > where('locations.lat','> ; $'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ',$ swlng)
- > where('users.id','=',$ user)
- > orWhere('shares.connected_user_id','=',$ user)
- > join('users','shares.user_id','=','users.id')
- > join('locations','locations.id','=' ,'users.location_id')
- > join('cities','provincial.id','=' locations.province_id')
- > join('countries','countries.id','=','locations.country_id')
- > select('shares.id AS share_id' ,'users.id AS user_id','shares.connected_user_id','shares.original_language_id','shares.image',
'users.first_name','users.last_name','users.email',
'locations.city','province.name','countries.code',
'locations.lat','locations.lng',
'shares.created_at')
- > raw('(CASE WHEN users.id ='。 $用户。 'THEN 1 ELSE 0 END)AS is_user')
- > orderBy('shares.created_at','desc')
- > orderBy('users.id','asc')
- > orderBy('shares.connected_user_id','asc')
- > get();
解决方案
移动你的 raw()
调用 SELECT
语句:
- > select('shares.id AS share_id','users.id AS user_id','shares.connected_user_id',
'shares.original_language_id','shares.image',
'users.first_name','users.last_name','users.email',
'locations.city','provincial.name','countries.code',
'locations.lat' ,'locations.lng',
'shares.created_at',
DB :: raw('(CASE WHEN users.id ='。$ user。'THEN 1 ELSE 0 END)AS is_user')
)
- > orderBy('shares.created_at','desc')
来自: http://four.laravel.com/docs/queries#raw-expressions
Is there anyone with experience in PHP & Laravel Eloquent who can help me resolve this statement? I'm trying to inject a CASE... WHEN.. END... inside a raw() method. It seemed like it was completely ignored. The existing documentation hasn't been . I've tried several different things to no prevail. I'm trying to pull this off:
SELECT shares.id, ...,
CASE WHEN users.id = <CurrentUser> THEN 1 ELSE 0 END AS is_user,
...
FROM <table>
...
The source code is below:
$shares = Share::where('shares.status', '=', SHARE_STATUS_APPROVED)
->where('shares.deleted', '=', '0')
->where('locations.lat', '<=', $nelat)
->where('locations.lat', '>=', $swlat)
->where('locations.lng', '>=', $nelng)
->where('locations.lng', '<=', $swlng)
->where('users.id', '=', $user)
->orWhere('shares.connected_user_id', '=', $user)
->join('users', 'shares.user_id', '=', 'users.id')
->join('locations', 'locations.id', '=', 'users.location_id')
->join('provinces', 'provinces.id', '=', 'locations.province_id')
->join('countries', 'countries.id', '=', 'locations.country_id')
->select('shares.id AS share_id', 'users.id AS user_id', 'shares.connected_user_id', 'shares.original_language_id', 'shares.image',
'users.first_name', 'users.last_name', 'users.email',
'locations.city', 'provinces.name', 'countries.code',
'locations.lat', 'locations.lng',
'shares.created_at')
->raw('(CASE WHEN users.id = ' . $user . ' THEN 1 ELSE 0 END) AS is_user')
->orderBy('shares.created_at', 'desc')
->orderBy('users.id', 'asc')
->orderBy('shares.connected_user_id', 'asc')
->get();
解决方案
Move your raw()
call inside the SELECT
statement:
->select('shares.id AS share_id', 'users.id AS user_id', 'shares.connected_user_id',
'shares.original_language_id', 'shares.image',
'users.first_name', 'users.last_name', 'users.email',
'locations.city', 'provinces.name', 'countries.code',
'locations.lat', 'locations.lng',
'shares.created_at',
DB::raw('(CASE WHEN users.id = ' . $user . ' THEN 1 ELSE 0 END) AS is_user')
)
->orderBy('shares.created_at', 'desc')
From: http://four.laravel.com/docs/queries#raw-expressions
这篇关于Laravel雄辩选择CASE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文