I want to make a laravel 9 app where the auth user can create folders and files (on DB level ... not physical) and work similar like windows explorer (creating folders, files, navigating) ... but I am stuck on DB creation.
The files can be:
versioned - and only the default marked is visibile
shared - if the user want to share - to set custom permission to it (ex: can_view or can_version the file)
I was thinking like this the DB Structure:
Setting for files and folders folder_id = 0 => means that they are on the "root" level (but this could be tricky - because there is no folder with ID 0)
ex: if files or folders have folder_id = 1 means that they are under the folder with ID 1
the list (array of files) need to be sorted by name (folders first then files -like in file manager) - and with pagination.
Any idea will be great on how can I start creating the migrations....
For ex: How can I query the DB to get this array:
$files = array(
['id' => 1, 'type' => 'folder', 'name' => 'Folder 1', 'info' => ['date' => '20.02.2022', 'size' => 1.2]],
['id' => 2, 'type' => 'folder', 'name' => 'Test Folder 2', 'info' => ['date' => '21.02.2022', 'size' => 1.2]],
['id' => 1, 'type' => 'file', fileType => 'excel', 'name' => 'Excel file', 'info' => ['date' => '21.02.2022', 'size' => 1.1, 'members' => 0]],
['id' => 2, 'type' => 'file', fileType => 'pdf', 'name' => 'Sample PDF File', 'info' => ['date' => '19.01.2022', 'size' => 1.3, 'members' => 3]],
);
All the files and folders that are on root level (folder_id = 0) with all the info mentioned and paginated ?
*** UPDATE ***
I have created the relations:
For model User:
public function files() {
return $this->hasMany(File::class);
}
public function folders() {
return $this->hasMany(Folder::class);
}
public function sharedFiles() {
return $this->belongsToMany(File::class, 'shared_files');
}
For File Model:
public function versions() {
return $this->hasMany(Version::class);
}
public function logs() {
return $this->hasMany(Log::class);
}
public function sharedUsers() {
return $this->belongsToMany(User::class, 'shared_files');
}
For Folder Model:
public function files() {
return $this->hasMany(Files::class);
}
public function parent() {
return $this->belongsTo(Folder::class, 'parent_id')->where('parent_id', 0)->with('parent');
}
public function children() {
return $this->hasMany(Folder::class, 'parent_id')->with('children');
}
For Log Model
public function file() {
return $this->belongsTo(File::class);
}
For Version Model:
public function file() {
return $this->belongsTo(File::class);
}