Dels Dels - 5 months ago 53
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:


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;

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);

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


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');


You can bypass instance creation with method injection

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