123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace App\Models\Service;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- class Project extends Model
- {
- use HasFactory;
- protected $table = 'service_project';
- protected $guarded = [];
- protected $appends = [];
- protected $casts = [
- 'isAdd' => 'bool',
- 'isStore' => 'bool',
- 'isDoor' => 'bool'
- ];
- public function getPriceAttribute($value)
- {
- return number_format($value / 100, 2,'.','');
- }
- public function getInitPriceAttribute($value)
- {
- return number_format($value / 100, 2,'.','');
- }
- public function getMaterialPriceAttribute($value)
- {
- return number_format($value / 100, 2,'.','');
- }
- public function getCoachProjectStatus($coach_id)
- {
- $status = DB::table('service_project_has_coach')->where('project_id', $this->attributes['id'])
- ->where('coach_id', $coach_id)->value('status');
- return $status ?: 0;
- }
- public function updateCoachProjectStatus(int $coach_id, int $status): void
- {
- $model = DB::table('service_project_has_coach');
- $project_id = $model->where('project_id', $this->attributes['id'])
- ->where('coach_id', $coach_id)->value('id');
- if($project_id)
- $model->where('id',$project_id)->update(['status' => $status]);
- else
- $model->insert(['project_id' => $this->attributes['id'], 'coach_id' => $coach_id, 'status' => $status]);
- }
- // 关联服务项目所属分类
- public function saveCategory(array $category_ids)
- {
- $data = collect($category_ids)->map(fn($category_id) => ['project_id' => $this->attributes['id'], 'category_id' => $category_id]);
- DB::table('service_project_has_category')->insert($data->all());
- }
- // 获取服务项目所属分类
- public function getCategory()
- {
- return DB::table('service_project_has_category')->where('project_id', $this->attributes['id'])->pluck('category_id');
- }
- public function deleteCategory()
- {
- return DB::table('service_project_has_category')->where('project_id', $this->attributes['id'])->delete();
- }
- // public function hasCoach()
- // {
- // return $this->hasOne(\App\Models\Coach\User::class,'user_id');
- //
- // }
- }
|