title: Laravel: how to set a custom column with conditions?
body: <p>Is possible with pure Eloquent uses, without php loops, add in a select statement a custom column and value to a relation just if the relation exists? I think that I don't know how to explain better, but I will try... </p>
  
  <p>The json result from my current query(eloquent) is the following json:</p>
  
  <pre><code>[
      {
          "id": 5,
          "user_id": 3,
          "category_id": 2,
          "city_id": 1,
          "title": "Outro teste",
          "body": "999999 sdf23asd f23asd32f1 as321f32as1d f1sdf",
          "image_path": "",
          "status": "ACTIVE",
          "expires_at": "2015-03-20 04:44:53",
          "popularity": 0,
          "is_favorite": "true",
          "category": {
              "id": 2,
              "name": "Categoria 2",
              "status": "ACTIVE",
              "color": "#0F0",
              "parent_id": null,
              "type": "INTEREST",
              "slug": "categoria-2",
              "icon_path": null
          },
          "favorite": []
      },
      {
          "id": 4,
          "user_id": 3,
          "category_id": 3,
          "city_id": 1,
          "title": "Interesse de Teste 2",
          "body": "2321fads132f123d sf 12sdf sd132",
          "image_path": "",
          "status": "ACTIVE",
          "expires_at": "2015-03-21 02:34:53",
          "popularity": 0,
          "is_favorite": "true",
          "category": {
              "id": 3,
              "name": "Subcategoria 1",
              "status": "ACTIVE",
              "color": "#00F",
              "parent_id": 1,
              "type": "INTEREST",
              "slug": "subcategoria-1",
              "icon_path": null
          },
          "favorite": [
              {
                  "id": 1,
                  "user_id": 3,
                  "favorite_id": 4,
                  "type": "INTEREST",
                  "created_at": null,
                  "updated_at": null
              }
          ]
      }
  ]
  </code></pre>
  
  <p>In the first index from results <code>is_favorite</code> field value must be "is_favorite": "false" because not <code>exists()</code> the <code>favorite</code> relation, and in the second index is_favorite value must be "is_favorite": "true" because exists the relation.</p>
  
  <p>My code to get this current json result I think that can be better... I'm noob in Laravel 5.0:</p>
  
  <pre><code>&lt;?php
  $Interests = \Reverse\Interest::with('category')
      -&gt;where('status', '&lt;&gt;', 'DELETED')
      -&gt;take($limit)
      -&gt;offset($offset);
  
  if (isset($allFilters['user'])) {
      $Interests-&gt;where('user_id', $allFilters['user']);
  }
  
  if (isset($allFilters['popularity'])) {
      $Interests-&gt;orderBy('popularity', 'desc')
          -&gt;orderBy('expires_at', 'asc');
  }
  
  if (isset($allFilters['favorites'])) {
      if($Interests-&gt;with('favorite')-&gt;exists()) {
          $Interests-&gt;select(DB::raw('*, "true" AS is_favorite'));
      }
  }
  
  $responseContent = $Interests-&gt;get();
  </code></pre>
  
  <p>Reverse is my API namespace. The json return is made with application/json passed on Response Header:</p>
  
  <pre><code>&lt;?php
  // My Json Response Example
  $Response = new \Illuminate\Http\Response();
  $Response-&gt;header('Content-Type', 'application/json');
  $Response-&gt;setContent($Interests-&gt;get());
  return $Response;
  </code></pre>