mardi 13 février 2018

Laravel select distinct value

I have the following request

$latestTrailers =  DB::table('game_trailer')
                        ->join('trailers', 'trailers.id', '=', 'game_trailer.trailer_id')
                        ->join('games', 'games.id', '=', 'game_trailer.game_id')
                        ->join('gamecoms', 'gamecoms.id', '=', 'games.gamecom_id')
                        ->join('platforms', 'platforms.id', '=', 'games.platform_id')
                        ->where('game_trailer.bool', 1)
                        ->where('trailers.bool', 1)
                        ->orderBy('game_trailer.created_at', 'desc')
                        ->select('name', 'platform_tag', 'trailer_link', 'name_url', 'platform_url', 'game_trailer.created_at')
                        ->limit(3)
                        ->get();

And the following result

    Collection {#407 ▼
  #items: array:3 [▼
    0 => {#420 ▼
      +"name": "Persona 5"
      +"platform_tag": null
      +"trailer_link": "cc-ClutaN_I"
      +"name_url": "persona-5"
      +"platform_url": "ps4"
      +"created_at": "2018-02-13 16:32:34"
    }
    1 => {#395 ▼
      +"name": "Persona 5"
      +"platform_tag": null
      +"trailer_link": "cc-ClutaN_I"
      +"name_url": "persona-5"
      +"platform_url": "ps3"
      +"created_at": "2018-02-13 16:30:36"
    }
    2 => {#427 ▼
      +"name": "Halo 3: ODST"
      +"platform_tag": null
      +"trailer_link": "Zz6FNKawJBc"
      +"name_url": "halo-3-odst"
      +"platform_url": "microsoft-xbox-360"
      +"created_at": "2018-02-13 16:27:56"
    }
  ]
}

I'd like the "trailer_link" value to be unique. And so make the request select the next entrie in the database if this column is not unique.

I tried the distinct() method but it doesn't work as I only want to use it on only one value.

Are there any equivalents ? Or another way to do it ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire