web-dev-qa-db-fra.com

Laravel Eloquent Select CASE?

Y a-t-il quelqu'un ayant de l'expérience en PHP & Laravel Eloquent qui peut m'aider à résoudre cette affirmation? J'essaie d'injecter un CASE ... QUAND .. END ... à l'intérieur d'une méthode raw (). Il semblait qu'elle était complètement ignorée. La documentation existante n'a pas été. J'ai essayé plusieurs choses différentes sans prévaloir. J'essaie de retirer ceci:

SELECT shares.id, ..., 
   CASE WHEN users.id = <CurrentUser> THEN 1 ELSE 0 END AS is_user, 
   ...
FROM <table>
...

Le code source est ci-dessous:

$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();
23
Coach Roebuck

Déplacez votre appel raw() à l'intérieur de l'instruction 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', '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')

De: https://laravel.com/docs/5.4/queries#raw-expressions

52
Alexandre Danault

Vous pouvez également utiliser selectRaw à la place.

->selectRaw("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,
      (CASE WHEN users.id = {$user} THEN 1 ELSE 0 END) AS is_user)")
    ->orderBy('shares.created_at', 'desc')
5
MM2