'array', // Convertimos el JSON en un arreglo automáticamente 'time_ranges' => 'array', // Convertimos el JSON en un arreglo automáticamente 'is_closed' => 'boolean' ]; // Asignar automáticamente el owner_id antes de crear un registro protected static function booted() { static::creating(function ($schedule) { if (Auth::check()) { // Verifica si el usuario está autenticado $user = Auth::user(); $business = $user->businesses->first(); // llama a la relación definida en el modelo User if ($business) { // dd($business); $schedule->business_id = $business->id; // Asigna el ID del business_id } } dd([ 'Datos del modelo antes de guardar:' => [ 'Atributos' => $schedule->getAttributes(), 'isDirty' => $schedule->isDirty(), 'Cambios' => $schedule->getDirty(), 'is_closed específicamente' => $schedule->is_closed ] ]); }); // Aplicar filtro para mostrar los servicios de la empresa a la que corresponde el usuario logeado static::addGlobalScope('business', function ($builder) { if (Auth::check()) { $user = Auth::user(); // dd($user); $business = $user->businesses->first(); // dd($business); if ($business) { $builder->where('business_id', $business->id); } else { $builder->where('business_id', null); } } }); } public function business(): BelongsTo { return $this->belongsTo(Business::class, 'business_id'); } public function services(): BelongsToMany { return $this->belongsToMany(Service::class, 'schedule_service')->withTimestamps(); } public function users(): BelongsToMany { return $this->belongsToMany(User::class, 'schedule_user')->withTimestamps(); } }