<?php

namespace App\Traits;

trait Owned
{

    public function scopeWhereOwned($_query, $_organisation_id = null)
    {
        if (is_null($_organisation_id)) {
            $_organisation_id = auth()->user()?->organisation_id;
            
            // Handle case where user is not authenticated
            if (is_null($_organisation_id)) {
                return $_query->whereRaw('1 = 0');
            }
        }
        
        return $_query->where('organisation_id', '=', $_organisation_id);
    }
    
}