osb/app/Traits/SiteID.php

35 lines
564 B
PHP
Raw Normal View History

2021-12-17 05:09:03 +00:00
<?php
/**
* Ensures we retrieve and save models with the correct site_id
*/
namespace App\Traits;
use Illuminate\Support\Arr;
use App\Models\Site;
2021-12-17 05:09:03 +00:00
use App\Models\Scopes\SiteScope;
trait SiteID
{
/**
* This model is site scoped
*/
protected static function booted()
{
static::addGlobalScope(new SiteScope);
}
public function save(array $options = [])
{
if (! Arr::get($this->attributes,'site_id'))
$this->site_id = config('site')->site_id;
return parent::save();
}
public function site()
{
return $this->belongsTo(Site::class);
}
2021-12-17 05:09:03 +00:00
}