<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Scopes\CompanyScope;
use Auth;

class ModelFilter extends Model
{

  public $defaultOrder = null;

  protected static function boot()
  {
    parent::boot();

    static::addGlobalScope(new CompanyScope);

    static::creating(function ($model) {

      $user = Auth::user();
      if ($user && $user->company_id) {
        $model->company_id = $user->company_id;
      } else {
        $model->company_id = 1;
      }
    });
  }

  public function company()
  {
    return $this->belongsTo('App\Company', 'company_id', 'company_id');
  }

  public function getDefaultOrder()
  {
    return $this->defaultOrder;
  }
}
