title: Laravel eloquent if 'anonym' field is 1, change 'username' to anonym
body: <p>I have a <code>Posts</code> table which contains a column <code>anonym</code> that can be either 1 or 0. I select the posts using Eloquent and join the user like this:</p>
  
  <pre><code>$posts = Post::with(
      array('user'=&gt;function($query){
          $query-&gt;select('id','username');
      }))
      -&gt;with('category')
      -&gt;withCount('likes')
      -&gt;orderBy('id', 'DESC')
      -&gt;paginate(25);
  </code></pre>
  
  <p>The return looks like this:</p>
  
  <pre><code>"data": [
      {
          "id": 116,
          "user_id": 3,
          ...
          "anonym": 1,
          "likes_count": 0,
          "user": {
              "id": 3,
              "username": "Max"
          },
          "category": null,
      },
  </code></pre>
  
  <p>Now, when the value of <code>anonym</code> in the posts table is <code>1</code>, I want the username not to be e.g. 'Max' but 'Anonym'. I tried it with a raw query, but I'm unable to get it to work.</p>
  
  <p>Is this even possible with eloquent?</p>
  
  <p>EDIT:</p>
  
  <p>I tried the following as suggested: </p>
  
  <pre><code>array('user'=&gt;function($query){
      $query-&gt;select(DB::raw("IF(`posts`.`anonym` = 0, `users`.`username`, 'Anonym') as username"));
  }))
  </code></pre>
  
  <p>but I get the error, that posts.anonym cannot be found: </p>
  
  <p>Column not found: 1054 Unknown column 'posts.anonym' in 'field list' (SQL: select IF(<code>posts</code>.<code>anonym</code> = 0, <code>users</code>.<code>username</code>, 'Anonym') as username from <code>users</code> where <code>users</code>.<code>id</code> in (1))</p>