[ 'label' => trans('Nom'), 'type' => CrudType::string(), 'validate' => [ 'string', 'max:255' ], 'required' => true, 'nullable' => false, 'actions' => [ CrudAction::viewAny(), CrudAction::view(), CrudAction::create(), CrudAction::update(), ] ], 'users' => [ 'label' => trans('Utilisateurs'), 'type' => CrudType::belongsToMany(), 'belongsToMany' => User::class, 'belongsToManyLabel' => 'name', 'nullable' => true, 'readonly' => function () { return auth()->user()->role->equals(Role::admin()) ? false : true; }, 'actions' => [ CrudAction::viewAny(), CrudAction::view(), CrudAction::create(), CrudAction::update(), ] ] ]; parent::__construct($attributes); } protected static function booted() { static::created(function (Project $project) { $path = Project::ticketAssetsFullPath($project->slug_name); if (!File::exists($path)) { File::makeDirectory($path, 0755, true); } }); static::updating(function (Project $project) { $oldName = Project::where('id', $project->id)->first()->name; $oldPath = Project::ticketAssetsFullPath($oldName); $newPath = Project::ticketAssetsFullPath($project->slug_name); if (File::exists($oldPath)) { File::move($oldPath, $newPath); } else { File::makeDirectory($newPath, 0755, true); } }); static::deleted(function (Project $project) { $path = Project::ticketAssetsFullPath($project->slug_name); if (File::exists($path)) { File::deleteDirectory($path); } }); } public function users() { return $this->belongsToMany(User::class); } public function tickets() { return $this->hasMany(Ticket::class, 'project_id'); } public function getSlugNameAttribute() { return Str::slug($this->name); } public static function ticketAssetsFullPath(string $str) { return public_path(self::ticketAssetsPath($str)); } public static function ticketAssetsPath(string $str) { return sprintf('fichiers/%s', $str); } }