64 lines
1.3 KiB
PHP
64 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models\FileManager;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Psy\Exception\ErrorException;
|
|
|
|
class Directory extends Model
|
|
{
|
|
/**
|
|
* @var bool $timstamps have created_at and updated_at fields
|
|
*/
|
|
public $timestamps = true;
|
|
protected $fillable = [
|
|
'name',
|
|
'description',
|
|
'parent_id',
|
|
'user_id',
|
|
'hidden'
|
|
];
|
|
protected $casts = [
|
|
'hidden' => 'bool',
|
|
'is_root' => 'bool'
|
|
];
|
|
|
|
/**
|
|
* Files in directory
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function files()
|
|
{
|
|
return $this->hasMany(File::class);
|
|
}
|
|
|
|
/**
|
|
* Relationship with parent/sub directories
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function allSubdirectories()
|
|
{
|
|
return $this->hasMany(Directory::class, 'parent_id', 'id');
|
|
}
|
|
|
|
public function subdirectories()
|
|
{
|
|
$subdirectories = $this->allSubdirectories()
|
|
->where('hidden', false);
|
|
|
|
return $subdirectories;
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
// try to prevent from deleting root folder
|
|
if ($this->is_root == true) {
|
|
throw new ErrorException('Cannot delete root folder!');
|
|
}
|
|
|
|
return parent::delete();
|
|
}
|
|
}
|