The route needs to be called with a DELETE header. If you open an url in the browser it will always be GET. It there for finds the first route that matches GET, which is the show route :)
Instead use that form you created
Hello community, As I am learning laravel I am unable to understand that when I am trying to access my delete.blade.php page by typing posts/delete in URL(that file is in resource/views/posts) it is calling the method show and printing "show" on that page. please guide me what I am doing wrong here
delete.blade.php
@extends('main')
@section('content')
<form method="POST" action="{{route('posts.destroy', '$post->id') }}" >
@method('DELETE')
@csrf
<select name="id">
<option value="1">vddv</option>
<option value="2">miss</option>
<option value="3">miss</option>
<option value="4">joy</option>
</select>
<br><br>
<button type="submit"> Delete blog</button>
</form>
@endsection
PostController.php (a resource controller)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
use Sessions;
class PostController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$post = new posts;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('posts/read');
}
public function show($data)
{
echo "show";
}
public function edit($id)
{
return view('posts.edit');
}
public function update(Request $req, $id)
{
echo posts::where('title' , $req->title)
->update(['body'=>$req->body]);
return redirect('/');
}
public function destroy($id)
{
$post = posts::find($id);
$post->delete();
return redirect('/');
}
}
route:
Route::resource('posts', 'PostController');
Please or to participate in this conversation.