Home > Mobile >  Why am i getting an error 404 when i run this code in laravel 9?
Why am i getting an error 404 when i run this code in laravel 9?

Time:08-11

I was watching an old tutorial about laravel 7 whiles using laravel 9, i tried to create a HTML form like this.

                    <div >
                    <form action="/upload" method="post" enctype="multipart/form-data">
                        @csrf
                        <input type="file" name="image">
                        <input type="submit" value="upload">
                    </form>   
                  </div>

then in my route(web.php) i added a code like this

route::post('/upload', function(Request $request)
{$request->image->store('images', 'public');
return 'image uploaded succesfully';

but in my webiste it tells me page the url you requested is not found on the site serve

CodePudding user response:

You've defined your route using POST meaning it will only respond to POST requests.

If you try to access /upload from a web browser, that uses a GET request which you've not defined a route for. So you want to define such a route:

Route::get('/upload', function () {
    return view('upload.blade.php');
});

You'll want to replace upload.blade.php with the name of your view that has your upload form in it.

CodePudding user response:

Name Your Route 1st , hit this command php artisan route:clear then try..

Change the form action action="{{ route('upload') }}"

 <div >
                    <form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
                        @csrf
                        <input type="file" name="image">
                        <input type="submit" value="upload">
                    </form>   
                  </div>
  • Related