﻿<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;
use Illuminate\Support\Facades\Auth;


class Category extends Model
{
protected $guarded  = [];
    use HasFactory;




    public function childs()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }
    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }
    public function courses()
    {
        return $this->hasMany(Course::class);
    }


protected static function booted()
    {

        static::addGlobalScope(new OrganisationScope);

        // Automatically set the organisation_id before creating the model
        static::creating(function ($model) {
                $model->organisation_id = Auth::user()->organisation_id;
                \Log::info('Setting organisation_id: ' . Auth::user());
        });
    }