[英]How can I delete sub folder by ID in Laravel?
I created a directory that I named directories that contains several sub directories.我创建了一个目录,我将其命名为包含多个子目录的目录。 I successfully created both directories database and directories path that has the related directories stored in the database.
我成功地创建了目录数据库和目录路径,其中相关目录存储在数据库中。
The codes below show both database and directories path created simultanuously:下面的代码显示了同时创建的数据库和目录路径:
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function add()
{
//Load directory view
return view('directories.add');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function insert(Request $request)
{
//validate directory name's field
$this->validate($request, [
'name' => 'required'
]);
//select the existing directory from database
$dir = Directory::where('name', '=', Input::get('name'))->first();
//get directory data
$directoryData = $request->all();
//retrieve the user session ID
$directoryData['user_id'] = Auth::user()->id;
//create a directory path name
$directory = 'directories/';
/**
* check if the directory path name exist
@param directory
*/
if(!File::exists($directory)){
//directory path name does not exist, we create new directory
Storage::makeDirectory($directory . $request->name);
//check if the directory record exist in the database. If it does
not exist, create a new one
if($dir === null){
//insert new directory
Directory::create($directoryData);
}
}else{
//if the above blocks return false, then do nothing.
Session::flash('');
}
//store status message
Session::flash('success_msg', 'Directory created successfully!');
return redirect()->route('directories.index');
}
Below are database directories created at the same time with sub directories in directories path:以下是与目录路径中的子目录同时创建的数据库目录:
The codes below with delete($id) method, delete only database directory.下面的代码使用 delete($id) 方法,只删除数据库目录。 But not sub directory:
但不是子目录:
public function delete($id)
{
$directory = 'directories/' . $id;
//delete database directory
Directory::find($id)->delete();
//delete sub directory
Storage::deleteDirectory('directories/' . $directory);
//store status message
Session::flash('success_msg', 'Directory deleted successfully!');
return redirect()->route('directories.index');
}
Below are sub directories that are subject to be deleted by ID:以下是被ID删除的子目录:
Does anyone have an idea on how to delete sub folder by id?有没有人知道如何通过 id 删除子文件夹? Because I tried to get:
$directory = 'directories/' . $id
因为我试图得到:
$directory = 'directories/' . $id
$directory = 'directories/' . $id
, but this doesn't work. $directory = 'directories/' . $id
,但这不起作用。
I finally made it with the code below:我终于用下面的代码做到了:
public function delete($id)
{
$directory = Directory::find($id);
Storage::deleteDirectory('directories/' . $directory->name);
//store status message
Session::flash('success_msg', 'Directory deleted successfully!');
return redirect()->route('directories.index');
}
This argument $directory->name
that I passed as sub directory name solved all this problem.我作为子目录名称传递的这个参数
$directory->name
name 解决了所有这些问题。 What a relief!终于解脱了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.