Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

showing me route errors 😒

my blade code

<form action="post">
                            @method('DELETE')
                            <td class="py-4 px-6">
                                <a href="{{ route('media.destroy',$photo->id ) }}" class="bg-red-600 p-2">
                                    Delete
                                </a>
                           
                            </td>    
                        </form>

my controller code

public function destroy($id)
    {
        $photos = Photo::findOrFail($id);
        unlink(public_path(),$photos->file);
        $photos->delete();
        return redirect('/admin/media');
        // return view('admin.media.index', compact('photos'));
    }

while I am deleting the image it takes me to the show method why ? showing this error

Method App\Http\Controllers\AdminMediasController::show does not exist.
0 likes
29 replies
Sinnbeck's avatar

Action is the route it should use. So /post. It's called method

And you can't use an a tag in a form like that. Use a button

And you cannot have a form inside a tr tag


                            <td class="py-4 px-6">
<form method="post" action="{{ route('media.destroy',$photo->id ) }}">
                            @method('DELETE')
                                <button type="submit" class="bg-red-600 p-2">
                                    Delete
                                </button>
                           </form>
                            </td>    
                         
Shivamyadav's avatar

@Sinnbeck I have deleted my categories through this code only , Lets have a look...

 @if($categories)
                        @foreach ($categories as $categorie )
                            <tr class="bg-teal-500 border-b ">
                                <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                                {{ $categorie->id}}
                                </th>
                                <td class="py-4 px-6">
                                    <a href="{{ route('categories.edit', $categorie->id)}}" class="text-blue-600 font-bold">
                                        {{ $categorie->name}}
                                    </a>
                                </td>
                            
                                <td class="py-4 px-6">
                                    {{$categorie->created_at ?  $categorie->created_at->diffForHumans() : 'No Date'}}
                                </td>
                                <td class="py-4 px-6">
                                    {{ $categorie->updated_at ? $categorie->updated_at->diffForHumans() : 'No Date'}}
                                </td>
                                <td class="py-4 px-6">
                                    <form action="{{ route('categories.destroy', $categorie->id)}}" method="Post">
                                        @method('DELETE')
                                        @csrf
                                        <button>
                                            Delete Categories
                                        </button>
                                        </form>
                                </td>
                            </tr>
                        @endforeach
                    @endif
Shivamyadav's avatar

@Sinnbeck yea everything is fine when i want to click on the delete button it takes me to the this method why ?

Method App\Http\Controllers\AdminMediasController::show does not exist.
Sinnbeck's avatar

@Shivamyadav well the error is saying that you are trying to make a get request it seems. But it should be post (delete)

Sinnbeck's avatar

Can you right click the delete button and select inspect element. Then copy the td with the form in and paste it here

1 like
Shivamyadav's avatar

@Sinnbeck this is that after Inspecting the button

<form action="POST">
 <input type="hidden" name="_method" value="DELETE">                               
 <a href="http://laravelproject.test/admin/media/3" class="bg-red-600 p-2">
                                    Delete
   </a>
 </form>
Sinnbeck's avatar

@Shivamyadav that's still an a tag not a button. Seems like the original code? Did you edit the wrong file?

Try clearing cache php artisan view:clear

Shivamyadav's avatar

@Sinnbeck done but error is same.. My whole controller

<?php

namespace App\Http\Controllers;

use App\Models\Photo;
use Illuminate\Http\Request;

class AdminMediasController extends Controller
{
    public function index()
    {
        $photos = Photo::all();
        return view('admin.media.index', compact('photos'));
    }

    public function create()
    {
        return view('admin.media.upload');
    }

    public function store(Request $request)
    {
        $files = $request->file('file');
        $name = time(). $files->getClientOriginalName();
        $files->move('images', $name);
        Photo::create(['file' => $name]);
        session()->flash('sucess', 'Images are uploaded!');
        return redirect('/admin/media');
    }

    // public function show($id)
    // {
        
    // }

    public function destroy($id)
    {
        $photos = Photo::findOrFail($id);
        unlink(public_path(),$photos->file);
        $photos->delete();
        return redirect('/admin/media');
        // return view('admin.media.index', compact('photos'));
    }
    
}

My whole blade index code

@extends('welcome')
@section('content')
<div class="shadow-md ">
    <table class="text-sm text-left text-gray-500 dark:text-gray-400 rounded-lg text-black w-3/6 lg:w-full">

        <thead class="text-xs text-gray-700 uppercase bg-black ">
            <tr class="text-white">
                <th scope="col" class="py-3 px-6">
                    ID
                </th>
                <th scope="col" class="py-3 px-6">
                    Name
                </th>
                {{-- <th scope="col" class="py-3 px-6">
                    User Name
                </th> --}}
                <th scope="col" class="py-3 px-6">
                    Created At
                </th>
                
            </tr>
        </thead>
        <tbody class="text-white">
            @if($photos)
                @foreach ($photos as $photo )
                    <tr class="bg-teal-500 border-b ">
                        <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                        {{ $photo->id}}
                        </th>
                        {{-- <td class="py-4 px-6">
                            <img height="50px" width="50px" src="/images/{{ $post->photo ? $post->photo->file : 'No photo found!'}}" alt="">
                            
                        </td> --}}
                        <td class="py-4 px-6">
                            <img  src="/images/{{ $photo ? $photo->file : 'No image' }}" alt="No image" width="200px">
                            
                        </td>
                        <td class="py-4 px-6">
                            {{ $photo->created_at ? $photo->created_at->diffForHumans() : 'No Date'}}
                        </td>
                       
                            <td class="py-4 px-6">
                                <form action="POST">
                                    @method('DELETE')
                                <a href="{{ route('media.destroy',$photo->id ) }}" class="bg-red-600 p-2">
                                    Delete
                                </a>
                            </form>
                            </td>    
                        
                        
                    </tr>
                @endforeach
            @endif
        </tbody>
    </table>
   
</div>
@endsection
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Shivamyadav so you forgot to change it?

This is the wrong code

<form action="POST">
                                    @method('DELETE')
                                <a href="{{ route('media.destroy',$photo->id ) }}" class="bg-red-600 p-2">
                                    Delete
                                </a>
                            </form>

Fixed again

<form method="POST" action="{{ route('media.destroy',$photo->id ) }}" >
                                    @method('DELETE')
                                <butypn class="bg-red-600 p-2">
                                    Delete
                                </button>
                            </form>
1 like
asims's avatar

@Shivamyadav as @sinnbeck has said you are using an anchor tag as a button. You need to use also your form is just hitting an action called POST which doesn’t seem right. You can use the route() helper in your blade for the action and point to media.destroy which I think is your route name. Not only that your media destroy route is set as a GET|HEAD not a DELETE.

See what you supplied: media.destroy â€ș AdminMediasController@destroy GET|HEAD admin/media/{medium}/edit

That should be a delete and not have /edit at the end.

Shivamyadav's avatar

@Sinnbeck done updated ! But the error is same

							<form action="POST">
                                    @method('DELETE')
                                    <button>
                                        <a href="{{ route('media.destroy',$photo->id ) }}" class="bg-red-600 p-2">
                                            Delete
                                        </a>
                                    </button>
                                </form>
asims's avatar

@Shivamyadav no you need to do something like this


<form action= "{{ route('media.destroy',$photo->id ) }}">
@method(‘DELETE’)
<button type=“submit”>Delete Photo</button>
</form>

Action is where you are sending the request to, it looks like you’ve added “POST” as the action when it was originally the method you was using.

Apologies @sinnbeck, didn’t see your code above.

1 like
Shivamyadav's avatar

@Sinnbeck Thanks a lot sir, form coding and college studying and excepting all these studies stuff I have to do my own works also so ,unable to getting the point what's going on ,my mind was just stuck their and can not think anything , It was an awkward moment for me :)

Sinnbeck's avatar

@Shivamyadav no worries :) I can suggest reading up on some forms and <a along with Http verbs like get, put, post, delete if you get the time :) they are important to know when working on web

1 like
MohamedTammam's avatar

You don't have method show in your AdminMediasController. Please show all your routes.

Shivamyadav's avatar

my routes

  GET|HEAD        admin/media .................................................................................................. media.index â€ș AdminMediasController@index
  POST            admin/media .................................................................................................. media.store â€ș AdminMediasController@store
  GET|HEAD        admin/media/create ......................................................................................... media.create â€ș AdminMediasController@create
  GET|HEAD        admin/media/{medium} ........................................................................................... media.show â€ș AdminMediasController@show
  PUT|PATCH       admin/media/{medium} ....................................................................................... media.update â€ș AdminMediasController@update
  DELETE          admin/media/{medium} ..................................................................................... media.destroy â€ș AdminMediasController@destroy
  GET|HEAD        admin/media/{medium}/edit ...................................................................................... media.edit â€ș AdminMediasController@edit

asims's avatar

@Shivamyadav See what you supplied:

media.destroy â€ș AdminMediasController@destroy GET|HEAD admin/media/{medium}/edit

That should be a delete as the method and not have /edit at the end.

Shivamyadav's avatar

My whole controller code goes here!

<?php

namespace App\Http\Controllers;

use App\Models\Photo;
use Illuminate\Http\Request;

class AdminMediasController extends Controller
{
    public function index()
    {
        $photos = Photo::all();
        return view('admin.media.index', compact('photos'));
    }

    public function create()
    {
        return view('admin.media.upload');
    }

    public function store(Request $request)
    {
        $files = $request->file('file');
        $name = time(). $files->getClientOriginalName();
        $files->move('images', $name);
        Photo::create(['file' => $name]);
        session()->flash('sucess', 'Images are uploaded!');
        return redirect('/admin/media');
    }

    // public function show($id)
    // {
        
    // }

    public function destroy($id)
    {
        $photos = Photo::findOrFail($id);
        unlink(public_path(),$photos->file);
        $photos->delete();
        return redirect('/admin/media');
        // return view('admin.media.index', compact('photos'));
    }
    
}

Please or to participate in this conversation.