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.
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>
@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 yes that looks correct. But are you saying it isn't working or?
@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.
@Shivamyadav that shouldn't happen when you have the @method ('DELETE'). Are you sure you are using the code above?
@Shivamyadav I don't know if it matters but try setting the method to uppercase
method="POST">
@Shivamyadav well the error is saying that you are trying to make a get request it seems. But it should be post (delete)
Can you right click the delete button and select inspect element. Then copy the td with the form in and paste it here
@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>
@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
@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
@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>
@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.
@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>
@Shivamyadav that is not at all the same code as mine? Maybe just copy mine?
@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.
@asims yeah ! done sir and its working ,
Thanks for yours efforts sir :)
@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
:)
@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
You don't have method show in your AdminMediasController. Please show all your routes.
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
@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.
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 sign in or create an account to participate in this conversation.