Dels Dels - 1 month ago 16
PHP Question

Laravel, unable to copyDirectory or moveDirectory

Currently I having problem with Laravel cannot use moveDirectory and copyDirectory however makeDirectory or deleteDirectory works fine, the code as follow:

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

use App\Http\Requests;

use App\Property;

use Intervention;

class PropertyController extends Controller {

public function update(Request $request, $id) {
$property = Property::findOrFail($id);
$oldName = $property->name;

$property->fill($request->all());
if ($request->name == '') {
$property->name = str_slug($request->title);
}
$property->updated_by = Auth::user()->username;

if ($oldName !== $property->name) {
Storage::disk('public')->moveDirectory('images/'.$oldName, 'images/'.$property->name);
}
$property->save();

show_feedback('info', 'Property successfully updated');

return redirect(route('property-management.index'));
}
}


with error:

BadMethodCallException in PluggableTrait.php line 85:
Call to undefined method League\Flysystem\Filesystem::moveDirectory

Answer

I guess moveDirectory is not available under Storage. Try with below code

import file system first

use Illuminate\Filesystem\Filesystem;

after that create a new instance and move directory. Path is relative to index.php I guess.

$file = new Filesystem();
$file->moveDirectory('../storage/app/public/old_folder', '../storage/app/public/new_folder');

Edited

You can bypass instance creation with method injection

public function update(Request $request,Filesystem $file, $id)