I was trying to link my posts.delete to my index.blade.php but I keep getting Symfony\Component\Routing\Exception\RouteNotFoundException
index.blade.php
@extends('layouts.app')
@section('content')
<div class="flex justify-center">
<div class="w-8/12 bg-white p-6 rounded-lg">
<form action="{{ route('posts') }}" method="post" class="mb-4">
@csrf
<div class="mb-4">
<label for="body" class="sr-only">Body</label>
<textarea name="body" id="body" cols="30" rows="4" class="by-gray-100 border-2
w-full p-4 rounded lg @error('body') border-red-500 @enderror" placeholder="Post something!"></textarea>
@error('body')
<div class="text-red-500 mt-3 text-sm">
{{$message}}
</div>
@enderror
</div>
<div>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded
font-medium">Post</button>
</div>
</form>
@if ($posts->count())
@foreach ($posts as $post)
<div class="mb-4">
<a href="" class="font-bold">{{ $post->user->username }}</a>
<span class="text-gray-600 text-sm">{{ $post->created_at->diffForHumans() }}</span>
<p class="mb-2">{{ $post->body }}</p>
<div class="flex items-center">
@if (!$post->likedBy(auth()->user()))
<form action="{{ route('posts.likes', $post) }}" method="post" class="mr-1">
@csrf
<button type="submit" class="text-blue-500">Like</button>
</form>
@else
<form action="{{ route('posts.destroy', $post) }}" method="delete" class="mr-1">
@csrf
@method('DELETE')
<button type="submit" class="text-blue-500">Unlike</button>
</form>
@endif
<span>{{ $post->likes->count() }} {{ Str::plural('likes', $post->likes->count()) }}</span>
</div>
</div>
@endforeach
{{ $posts->links() }}
@else
There are no posts...
@endif
</div>
</div>
@endsection
PostLikeController
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostLikeController extends Controller
{
public function __construct()
{
$this->middleware(['auth']);
}
public function store(Post $post, Request $request)
//While trying to add $request here it gave back an Undefined variable '$request'.
//But after adding Request $request to it, it worked...
{
if ($post->likedBy($request->user())){
return response(null, 409);
}
$post->likes()->create([
'user_id' => $request->user()->id,
]);
return back();
}
public function destroy(Post $post, Request $request)
{
dd($post);
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
use App\Http\Controllers\PostLikeController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\LogoutController;
use App\Http\Controllers\Auth\RegisterController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
//return view('welcome');
});
// Route::get('/', function () {
// return view('posts.index');
// });
Route::get('/', function () {
return view('home');
})->name('home');
Route::get('/dashboard', [DashboardController::class, 'index'] )
->name('dashboard')
->middleware('auth');
Route::get('/register', [RegisterController::class, 'index'] )->name('register');
Route::post('/register', [RegisterController::class, 'store'] );
Route::get('/login', [LoginController::class, 'index'] )->name('login');
Route::post('/login', [LoginController::class, 'store'] );
Route::post('/logout', [LogoutController::class, 'store'] )->name('logout');
Route::get('/login', [LoginController::class, 'index'] )->name('login');
Route::get('/posts', [PostController::class, 'index'] )->name('posts');
Route::post('/posts', [PostController::class, 'store'] );
Route::post('/posts/{post}/likes', [PostLikeController::class, 'store'] )->name('posts.likes');
Route::delete('/posts/{post}/likes', [PostLikeController::class, 'destroy'])->name('posts.likes');
Like.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
use HasFactory;
protected $fillable = [
'user_id'
];
}
I checked my routes:list and I found that it was there
+--------+----------+--------------------+-------------+----------------------------------------------------+------------+
| Domain | Method | URI | Name | Action
| Middleware |
+--------+----------+--------------------+-------------+----------------------------------------------------+------------+
| | GET|HEAD | / | home | Closure
| web |
| | GET|HEAD | api/user | | Closure
| api |
| | | | |
| auth:api |
| | GET|HEAD | dashboard | dashboard | App\Http\Controllers\DashboardController@index | web |
| | | | |
| auth |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@index | web |
| | | | |
| guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController@store | web |
| | | | |
| guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LogoutController@store | web |
| | GET|HEAD | posts | posts | App\Http\Controllers\PostController@index | web |
| | POST | posts | | App\Http\Controllers\PostController@store | web |
| | POST | posts/{post}/likes | posts.likes | App\Http\Controllers\PostLikeController@store | web |
| | | | |
| auth |
| | DELETE | posts/{post}/likes | posts.likes | App\Http\Controllers\PostLikeController@destroy | web |
| | | | |
| auth |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@index | web |
| | | | | | guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@store | web |
| | | | | | guest |
+--------+----------+--------------------+-------------+----------------------------------------------------+------------+
N.B: I was trying to make it destroy the Likes that has been created but it keeps giving Symfony\Component\Routing\Exception\RouteNotFoundException Route [posts.destroy] not defined. (View: C:\xampp\htdocs\posty\resources\views\posts\index.blade.php)