简体   繁体   中英

Laravel - Return Custom Extra Data in Eloquent Model

I have a Advertisement Eloquent Model like this-

<?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->belongsTo('App\User','user_id', 'id');
    }

    public function UserAdvertisementView()
    {
        return $this->hasMany('App\UserAdvertisementView', 'add_id', 'id');
    }
}

So it is linked with UserAdvertisementView Eloquent Model . So, UserAdvertisementView is like this-

<?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'
                                ];
}

So, when I am using this in controller-

return Advertisement::with('UserAdvertisementView')->get();

And getting something like this-

[
  {
    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
      }
    ]
  }
]

But I like to have something like this-

[
  {
    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
    ]
  }
]

So, I want to have the SUM of total_count (2+1 = 3) for all users.

So, I need to create a custom query in the Eloquent Model .

Is there any way for this?


Update-

According to @Jeff, I have done added this to Advertisement -

public $appends = ['total_views'];

public function getTotalViewsAttribute()
{
    return $this->UserAdvertisementView->sum('total_view');
}

And then made a query like this in controller-

return Advertisement::get();

And having this-

在此处输入图片说明

So, I am having some extra data which is bad for a better performance when I am working with big data.

So, is there any way to have that extra part removed and just get what we need.

Or is there any way to have custom query in with clouse in Eloquent Model?


Thanks in advance for helping.

On your Advertisement model, you can add:

public $appends = ['total_views'];

public function getTotalViewsAttribute(){
  return $this->UserAdvertisementView->sum('total_view');
}

This will automatically append the total_views attribute to your Advertisement model when it is sent to JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM