Class 'App\Http\Controllers\Storage' not found

Published 4 months ago by sadok

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use App\User;
use App\Team;
use App\Club;

class TeamController extends Controller
{

public function destroy($id)
  {
    $team=Team::findOrFail($id);
    $team->delete();
    Storage::delete($team->logo);
    return response()->json(['data' => $team],200);
  }
}
Best Answer (As Selected By sadok)
Cronix

This code is also in the wrong order...

$team=Team::findOrFail($id);
$team->delete();
\Storage::delete($team->logo);

You need to delete the $team->logo before you delete the $team. If you delete $team first, $team->logo doesn't exist any longer.

// import the storage facade
use Illuminate\Support\Facades\Storage;

public function destroy($id)
{    
    $team=Team::findOrFail($id);
    Storage::delete($team->logo);
    $team->delete();

    return response()->json(['data' => $team], 200);
}
sadok

now it's returning 404

xLukas

If you don't import your class at the beginning of the file and use it like this Storage::delete($team->logo); autoloader tries to find the class relatively to your current namespace. What is in your case App\Http\Controllers so autoloader look for class App\Http\Controllers\Storage . If you don't want autoloader look for class relatively to your current namespace you can add backslash in front of your class \Storage::delete($team->logo) or just import the class at the beginning of the file use Storage; and use it like you did. Checkout this link to learn more about namespaces PHP namespaces

sadok

i did try " use Storage " and it still returning 404 :(

rin4ik
rin4ik
4 months ago (266,140 XP)

with this?

$team=Team::findOrFail($id);
    \Storage::delete($team->logo);
    $team->delete();
    return response()->json(['data' => $team],200);
Dry7
Dry7
4 months ago (152,030 XP)

or app('filesystem')->delete($team->logo)

sadok

yes i tried that still the same issue and i ve allready run the cmd php artisan storage:link and nothing of these tries solved my issue

rin4ik
rin4ik
4 months ago (266,140 XP)

pls dd after each step

$team=Team::findOrFail($id);
dd('ok');    
\Storage::delete($team->logo);
    $team->delete();
    return response()->json(['data' => 'deleted'],200);

I think it can't find the team with that id . maybe you are trying to remove already removed team

xLukas

The reason why you have 404 can be several.

  1. Your destroy controller method "listen" for DELETE request but you do different type of request
  2. You using findOrFail($id) method which gives you 404 when model with given ID is not in DB.

Try to do dd($id) at the very beginning of the destroy method so we can see if it even hits your method.

sadok

no i checked my data base and the team still exists

sadok

it returns the team s id

rin4ik
rin4ik
4 months ago (266,140 XP)

@sadok what do you have after this?

$team=Team::findOrFail($id);
dd($team);
sadok
  public function destroy($id)
  {
    dd($id);
    $team=Team::findOrFail($id);
    $team->delete();
    \Storage::delete($team->logo);
    return response()->json(['data' => $team],200);
  }
sadok

in postman i can see it returning the team id that i want to delete , but it doesn't get deleted

sadok

@rin4ik the problem is that Storage can't be found witch is not logic , that's so weird, it's driving me crazy

xLukas

HTML forms cannot do DELETE request so you need to do form method spoofing manually. Postman can do DELETE.

<form action="YOUR_DELETE_ROUTE" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <input type="submit">
</form>

Form Method Spoofing

Please sign in or create an account to participate in this conversation.