store-manage/app/Models/Store.php

73 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Enums\BusinessStatus;
use Illuminate\Database\Eloquent\Casts\Attribute;
use EloquentFilter\Filterable;
use App\Traits\HasDateTimeFormatter;
/**
* 门店
*/
class Store extends Model
{
use Filterable, HasDateTimeFormatter;
protected $fillable = ['title', 'master_id', 'category_id', 'business_id', 'level_id', 'region', 'lon', 'lat', 'profit_ratio', 'profit_money', 'business_status'];
protected $casts = [
// 地区 {province: 四川省, city: 成都市}
'region' => 'json',
'business_status' => BusinessStatus::class,
];
protected $appends = ['business_status_text', 'business_status_color'];
// 店长
public function master()
{
return $this->belongsTo(Employee::class, 'master_id');
}
// 门店分类
public function category()
{
return $this->belongsTo(Keyword::class, 'category_id', 'key');
}
// 经验分类
public function business()
{
return $this->belongsTo(Keyword::class, 'business_id', 'key');
}
// 门店等级
public function level()
{
return $this->belongsTo(Keyword::class, 'level_id', 'key');
}
// 门店员工
public function employees()
{
// role(1: 店长, 2: 店员)
return $this->belongsToMany(Employee::class, 'store_employees', 'store_id', 'employee_id')->withPivot(['role']);
}
protected function businessStatusText(): Attribute
{
return new Attribute(
get: fn () => $this->business_status ? $this->business_status->text() : '',
);
}
protected function businessStatusColor(): Attribute
{
return new Attribute(
get: fn () => $this->business_status ? $this->business_status->color() : '',
);
}
}