File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Businesses.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
use App\BusinessCategory;
use App\Address;
use App\BusinessUserRating;
class Businesses extends Model
{
protected $fillable = [
'name',
'description',
'phone',
'age',
'instagram',
'facebook',
'user_id',
'category_id',
'address_id',
'average-rating',
'website',
'country_code',
'dial_code',
];
// protected $appends = [
// 'average-rating'
// ];
public function user()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->belongsTo(BusinessCategory::class);
}
public function address()
{
return $this->belongsTo(Address::class);
}
public function rating()
{
return $this->hasMany(BusinessUserRating::class, 'business_id');
}
public function getAverageRatingAttribute()
{
return round($this->rating()->avg('score'),1);
}
}