1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Models;
- use App\Models\Enums\AssetACL;
- use App\Models\Scopes\CompanyScope;
- use EloquentFilter\Filterable;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Auth;
- /**
- * @method static \Illuminate\Database\Eloquent\Builder allowed()
- */
- class Asset extends Model
- {
- use HasFactory, Filterable;
- protected $fillable = [
- "name", "code", "description", "company_id", "status", "created_by",
- "owner", "address", "group_id", "geo_address_code", "acl",
- "whitelist", "latitude", "longitude","parent_id", "path",
- ];
- protected static function booted(): void
- {
- static::addGlobalScope(new CompanyScope);
- }
- public function scopeAllowed(Builder $query): void
- {
- $query->where(function (Builder $query) {
- return $query->where('acl', AssetACL::PRIVATE->value)->where('owner', Auth::id());
- })->orWhere(function (Builder $query) {
- return $query->where('acl', AssetACL::CUSTOM->value)->where('whitelist', 'like', '%' . Auth::id() . '%');
- });
- }
- public function children(){
- return $this->hasMany(Asset::class ,'parent_id');
- }
- public function requirements(){
- return $this->hasMany(Requirement::class ,'asset_id');
- }
- public function plans(){
- return $this->hasMany(Plan::class ,'asset_id');
- }
- public function projects(){
- return $this->belongsToMany(Project::class,'project_asset');
- }
- }
|