belongsTo(User::class, 'created_by'); } // returns the parent file or folder for this file or folder public function parent(): BelongsTo { return $this->belongsTo(File::class, 'parent_id'); } // returns whether or not the current folder is a root-level folder public function isRoot() { // is there a parent_id for this folder? return $this->parent_id == null; } // returns simple boolean whether or not the passed userID is the creator of the file public function isOwner($userID): bool { return $this->created_by = $userID; } // additional bootstrapping on top of default Model model protected static function boot() { // start with parent Model parent::boot(); // define new bootstrapping static::creating(function ($model) { // check for parent - if one exists, exit function if (!$model->parent) return; // if file or folder is NOT a root-level folder if (!$model->parent->isRoot()) { // path is a node off the parent path $model->path = $model->parent->path . '/'; } // otherwise free-standing else $model->path = ''; // append current file or folder name to path name $model->path = $model->path . Str::slug($model->name); }); } }