Lawrence E Bosumbe
Lawrence E Bosumbe

Reputation: 582

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:

directory list

The codes below with delete($id) method, delete only database directory. 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:

sub directory

Does anyone have an idea on how to delete sub folder by id? Because I tried to get: $directory = 'directories/' . $id, but this doesn't work.

Upvotes: 1

Views: 365

Answers (1)

Lawrence E Bosumbe
Lawrence E Bosumbe

Reputation: 582

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. What a relief!

Upvotes: 1

Related Questions