title: Laravel - Return Custom Extra Data in Eloquent Model
body: <p>I have a Advertisement <a href="https://laravel.com/docs/5.1/eloquent" rel="nofollow noreferrer">Eloquent Model</a> like this-</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Advertisement extends Model
  {
      protected $dates        =   ['deleted_at'];
  
      protected $table        =   'advertisements';           //Table Name
  
      protected $fillable     =   [
                                      'user_id',
                                      'category_id',
                                      'sub_category_id',
                                      'title',
                                      'price',
                                      'description',
                                      'address',
                                      'location_lat',
                                      'location_lon',
                                      'is_active',
                                      'deleted_at'
                                  ];
  
      protected $hidden = [
                              'user_id',
                              'category_id',
                              'sub_category_id',
                              'deleted_at',
                              'created_at',
                              'updated_at',
                              'is_active',
                          ];
  
      public function User()
      {
          return $this-&gt;belongsTo('App\User','user_id', 'id');
      }
  
      public function UserAdvertisementView()
      {
          return $this-&gt;hasMany('App\UserAdvertisementView', 'add_id', 'id');
      }
  }
  </code></pre>
  
  <p>So it is linked with <code>UserAdvertisementView</code> <a href="https://laravel.com/docs/5.1/eloquent" rel="nofollow noreferrer">Eloquent Model</a>. So, <code>UserAdvertisementView</code> is like this-</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class UserAdvertisementView extends Model
  {
      protected $primaryKey   =   null;
      public $incrementing    =   false;
      public $timestamps      =   false;
  
      protected $table        =   'user_add_views';           //Table Name
  
      protected $fillable     =   [
                                      'user_id',
                                      'add_id',
                                      'total_view'
                                  ];
  }
  </code></pre>
  
  <p>So, when I am using this in controller-</p>
  
  <pre><code>return Advertisement::with('UserAdvertisementView')-&gt;get();
  </code></pre>
  
  <p>And getting something like this-</p>
  
  <pre><code>[
    {
      id: 1,
      title: "as dasd as",
      price: 0,
      description: "asd asdasdasd",
      address: "Khagrachhari, Chittagong Division, Bangladesh",
      location_lat: 23.13,
      location_lon: 91.95,
      user_advertisement_view: [
        {
          user_id: 1,
          add_id: 1,
          total_view: 1
        },
        {
          user_id: 16,
          add_id: 1,
          total_view: 2
        }
      ]
    }
  ]
  </code></pre>
  
  <p>But I like to have something like this-</p>
  
  <pre><code>[
    {
      id: 1,
      title: "as dasd as",
      price: 0,
      description: "asd asdasdasd",
      address: "Khagrachhari, Chittagong Division, Bangladesh",
      location_lat: 23.13,
      location_lon: 91.95,
      user_advertisement_view: [
        total_view: 3
      ]
    }
  ]
  </code></pre>
  
  <p>So, I want to have the <strong>SUM</strong> of <code>total_count</code> <code>(2+1 = 3)</code>  for all users.</p>
  
  <p>So, I need to create a custom query in the <a href="https://laravel.com/docs/5.1/eloquent" rel="nofollow noreferrer">Eloquent Model</a>.</p>
  
  <p>Is there any way for this?</p>
  
  <hr>
  
  <h2>Update-</h2>
  
  <p>According to @Jeff, I have done added this to <code>Advertisement</code>-</p>
  
  <pre><code>public $appends = ['total_views'];
  
  public function getTotalViewsAttribute()
  {
      return $this-&gt;UserAdvertisementView-&gt;sum('total_view');
  }
  </code></pre>
  
  <p>And then made a query like this in controller-</p>
  
  <pre><code>return Advertisement::get();
  </code></pre>
  
  <p>And having this-</p>
  
  <p><a href="https://i.stack.imgur.com/d0a0t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d0a0t.png" alt="enter image description here"></a></p>
  
  <p>So, I am having some extra data which is bad for a better performance when I am working with big data.</p>
  
  <p>So, is there any way to have that extra part removed and just get what we need.</p>
  
  <p>Or is there any way to have custom query in <code>with</code> clouse in Eloquent Model?</p>
  
  <hr>
  
  <p>Thanks in advance for helping.</p>