<?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(
                    $builder->getModel()->getTable() . '.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);
    }
}