<?php

namespace App\Trait;

use App\Models\Solicitor\Solicitor;

trait  BelongsToSolicitor {
    protected static function bootBelongsToSolicitor()
    {
        if (app()->runningInConsole()) {
            return;
        }

        static::addGlobalScope('solicitor', function ($builder) {
            $solicitor = app(Solicitor::class);

            if ($solicitor && $solicitor->id) {
                $builder->where('solicitor_id', $solicitor->id);
            }
        });


        static::creating(function ($model) {
            $solicitor = app(Solicitor::class);


            if ($solicitor && empty($model->solicitor_id)) {
                $model->solicitor_id = $solicitor->id;
            }
        });
    }

    public function solicitor() {
        return $this->belongsTo(Solicitor::class);
    }
}