skip to Main Content

i know it’s a silly issue but I am at the very beginning stage of learning Laravel without any guidance or help from anyone. so please, don’t mind. I’ll delete it after getting the answer.

I am using Laravel with react inertia. i am submitting a form from ‘/p/create’ to ‘/p’ route that is returning me 404 not found error. can’t find what went wrong here.

My routes:

Route::get('/p/create', function () {
    return Inertia::render('Posts');
})->name('posts.create');

Route::post('/p', [PostController::class, 'store'])->name('posts.store');

my PostController:

<?php

namespace AppHttpControllers;

use AppHttpControllersController;
use IlluminateHttpRequest;

class PostController extends Controller
{

    public function store()
    {
        dd(request()->all())
    }
}

The form of ‘/p/create’


export default function Posts({ auth }) {
    return (
        <div className="post">
            <Navbar auth={auth} />

            <form
                encType="multipart/form-data"
                action="{{ route('posts.store') }}"
                method="POST"
            >
                <input
                    type="hidden"
                    name="_token"
                    value="{!! csrf_token() !!}"
                />

                <input type="file" name="image" />
                <textarea name="caption" />
                <button type="submit">Post</button>
            </form>
        </div>
    );
}

my route List:

PS E:PHP projectsnew and finalbackInstagram> php artisan route:list

  GET|HEAD  / .................................................................................. generated::1UHNC2h1AcGloyZQ
  POST      _ignition/execute-solution ....... ignition.executeSolution › SpatieLaravelIgnition › ExecuteSolutionController  
  GET|HEAD  _ignition/health-check ................... ignition.healthCheck › SpatieLaravelIgnition › HealthCheckController  
  POST      _ignition/update-config ................ ignition.updateConfig › SpatieLaravelIgnition › UpdateConfigController
  GET|HEAD  confirm-password .................................... password.confirm › AuthConfirmablePasswordController@show  
  POST      confirm-password ........................ generated::vG6ZS3cMZcBju2hz › AuthConfirmablePasswordController@store  
  GET|HEAD  dashboard ............................................................................................ dashboard  
  GET|HEAD  dashboard/{user} ................................................................................ dashboard.user  
  POST      email/verification-notification ......... verification.send › AuthEmailVerificationNotificationController@store  
  GET|HEAD  forgot-password ..................................... password.request › AuthPasswordResetLinkController@create  
  POST      forgot-password ........................................ password.email › AuthPasswordResetLinkController@store  
  GET|HEAD  login ....................................................... login › AuthAuthenticatedSessionController@create  
  POST      login .................................. generated::WxuQbzVnBo7s2ikB › AuthAuthenticatedSessionController@store  
  POST      logout .................................................... logout › AuthAuthenticatedSessionController@destroy  
  POST      p ........................................................................... posts.store › PostController@store  
  GET|HEAD  p/create .......................................................................................... posts.create
  PUT       password ...................................................... password.update › AuthPasswordController@update  
  GET|HEAD  profile .................................................................. profile.edit › ProfileController@edit  
  PATCH     profile .............................................................. profile.update › ProfileController@update  
  DELETE    profile ............................................................ profile.destroy › ProfileController@destroy  
  GET|HEAD  register ....................................................... register › AuthRegisteredUserController@create  
  POST      register ..................................... generated::UZNwLO2B8FS0PVb4 › AuthRegisteredUserController@store  
  POST      reset-password ............................................... password.store › AuthNewPasswordController@store  
  GET|HEAD  reset-password/{token} ...................................... password.reset › AuthNewPasswordController@create  
  GET|HEAD  sanctum/csrf-cookie .......................... sanctum.csrf-cookie › LaravelSanctum › CsrfCookieController@show  
  GET|HEAD  up ................................................................................. generated::pquyE3RwESLgrYla
  GET|HEAD  verify-email ...................................... verification.notice › AuthEmailVerificationPromptController  
  GET|HEAD  verify-email/{id}/{hash} ...................................... verification.verify › AuthVerifyEmailController  

3

Answers


  1. You defined your "post" route without a name attribute. Change it to this:

    Route::post('/p', [PostController::class, 'store'])->name('posts.store');
    

    And then, you will be able to use it like this:

    action="{{ route('posts.store') }}"
    

    Read more here: https://laravel.com/docs/11.x/routing#named-routes

    Login or Signup to reply.
  2. You missed your route name, just change your route to this:

    Route::post('/p', [PostController::class, 'store'])->name('posts.store');
    
    Login or Signup to reply.
  3. Here is a solution that is working for me.

    // Controller call in route file

    use AppHttpControllersPostController;
    

    // Route define

    Route::post("/p", [PostController::class, 'store'])->name('store');
    

    // Use in view file

    action="{{ route('store') }}"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search