File: //home/armgroup/libs/app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, HasRoles, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
'mobile',
'active',
'is_seller',
'email_verified_at',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function getAvatarAttribute()
{
return asset('images/defaults/user.png');
}
public function getCreditAttribute(): int
{
return $this->wallets()->sum('amount');
}
public function getPointAttribute(): int
{
return $this->points()->sum('amount');
}
public function pages()
{
return $this->hasMany(Page::class);
}
public function addresses()
{
return $this->morphToMany(Address::class, 'addressable');
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function wallets()
{
return $this->hasMany(Wallet::class);
}
public function points()
{
return $this->hasMany(Point::class);
}
public function favorites()
{
return $this->hasMany(Favorite::class);
}
public function scores()
{
return $this->hasMany(Score::class);
}
public function orders()
{
return $this->hasMany(Order::class);
}
public function products()
{
return $this->hasMany(Product::class);
}
public function demands()
{
return $this->belongsToMany(Product::class, 'product_demands')->using(ProductDemand::class);
}
public function hasDemanded($product_id, $value_id): bool
{
return DB::table('product_demands')
->where('user_id', $this->id)
->where('product_id', $product_id)
->when($value_id, function ($query) use ($value_id) {
return $query->where('value_id', $value_id);
})
->exists();
}
public function feedback()
{
return $this->hasMany(Feedback::class);
}
public function payments()
{
return $this->hasMany(Payment::class);
}
public function sellerInformation()
{
return $this->hasOne(SellerInformation::class);
}
}