Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 404 – not found

Sorry im new to the coding world – I was creating an admin user and after completing the admin, my comment section which was fully functional stopped working and every time i go to post a comment i get the error 404 not found which i know has to do with routing – i have used the routes commands already to try and fix the problem and to no avail – many thanks for looking at my problem!

photo.blade.php – comment area

<div>
                <form action="{{$photo->id}}/comments" method="POST" class="mb-0">
                  <input type="hidden" name="post_slug" value="{{$photo->id}}">
                   @csrf

                   <label class="mt-6 block text-sm font-medium text-gray-700">Comment</label>
                   <textarea name="comment_body" class="mt-1 py-2 px-3 block w-full borded border-gray-400 rounded-md shadow-sm" required>{{old('comment_body')}}</textarea>

                    <button
                     class="bg-laravel text-white rounded py-2 px-4 mt-6 hover:bg-black"
                     >
                     Post Comment
                      </button>
                  </form>
              </div>

Comments controller

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php

namespace App\Http\Controllers;

use App\Models\Photos;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\CommentsRequest;
use Illuminate\Support\Facades\Validator;

class CommentsController extends Controller
{
    //Store Comments
    public function store(Request $request)
    {
        if(Auth::check())
        {
            $validator = Validator::make($request->all(), [
                'comment_body' => 'required|string'
            ]);

            if($validator->fails())
            {
                return redirect()->back()->with('message', 'Comment area is mandatory');
            }

            $photo = Photos::where('id', $request->post_slug)->first();
            if($photo)
            {
                Comment::create([
                    'photos_id' => $photo->id,
                    'user_id' => Auth::user()->id,
                    'comment_body' => $request->comment_body
                ]);

                return redirect()->back()->with('message', 'Comment successfully posted!');
            }
            else
            {
               return redirect()->back()->with('message', 'No Post Found!');
            }
        }
        else
        {
            return redirect('login')->with('message', 'Login first to log in!');
        }
    }

    //Edit form
    public function edit(Comment $comment) {
        return view('edit-comments', ['comments' => $comment]);
    }

    //update comment
    public function update(Request $request, Comment $comment) {
        $formFields = $request->validate([
            'comment_body' => 'required'
        ]);

        $comment->update($formFields);


        return redirect()->back()->with('message', 'Post updated successfully!');
    }


    //Delete Listing
    public function destroy(Comment $comment){
        $comment->delete();
        return redirect()->back()->with('message', 'Comment deleted successfully!');
    }
}

Comments routing

//Comments route
Route::post('/comments/{comment}',
[CommentsController::class, 'store']);

//Delete Comments
Route::delete('/comments/{comment}',
[CommentsController::class, 'destroy'])->name('comments.destroy')->middleware('auth');

//Edit form - Comments
Route::get('/comments/{comment}/edit',
[CommentsController::class, 'edit'])->middleware('auth');

//Update comments
Route::put('/comments/{comment}',
[CommentsController::class, 'update'])->middleware('auth');

if there is any other code need feel free to comment under the post again – many thanks!

>Solution :

Based on the code you provided, it seems that the issue might be with your comments route. You are using the same URL parameter "comment" for both the "store" and "destroy" methods in the CommentsController. This might be causing a conflict with the routing, which can lead to a 404 error.

To fix this issue, you can change the parameter name for the "store" method to something else, such as "photo", like this:

Route::post('/photos/{photo}/comments', [CommentsController::class, 'store']);

Then, you need to update your comment area form to use the correct URL for posting comments:

<form action="/photos/{{$photo->id}}/comments" method="POST" class="mb-0">

Essentially you have the routes for /comments/{comment.id} but your form is sending the request to {{$photo->id}}/comments. Line up your call to the route and all will work as expected.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading