Sent post request but got get response in Laravel - laravel

This is blade template:
<form action="/fileupload" action="post" enctype="multipart/form-data">
#csrf
<input type="file" name="file"><br>
<button type="submit">submit</button>
</form>
This is the route:
Route::get('/fileupload', [FileUpload::class,"upload"]);

Related

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

I was watching an old tutorial about laravel 7 whiles using laravel 9, i tried to create a HTML form like this.
<div class="card-body">
<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
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.
Name Your Route 1st , hit this command php artisan route:clear then try..
Change the form action action="{{ route('upload') }}"
<div class="card-body">
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>

Change PUT request to POST request to include image update

I wrote a post edit method which at first consisted only of text. I did the update using a PUT request.
Now I want to include images to my posts, so I added it, and it works for my POST request of creating a post but doesn't work for my update post, when I want to change image, since PUT request doesn't support file uploads.
So now I'm stuck trying to change my update method from PUT request that only updates text to a POST request that updates both the text and an image if supplied.
This is the code I wrote so far:
public function update(Request $request, Post $post)
{
//store updated image
if($request->hasFile('image') && $request->file('image')->isValid()){
if($post->hasMedia('posts')) {
$post->media()->delete();
}
$post->addMediaFromRequest('image')->toMediaCollection('post', 's3');
}
$post->update(request()->validate([
'body' => 'required'
]));
return redirect($post->path());
}
I think that the $post->update doesn't work for the POST request. I just want to update a text if an update was given.
Using Laravel 6.
EDIT: My form layout structure (simplified)
<form action="action="/posts/{post}" method="POST">
#method('PUT')
#csrf
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
My routes:
Route::get('/posts/{post}/edit', 'PostsController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostsController#update');
I tried your code and it worked fine with me , only added #csrf in form tag.
<form action="/posts/{post}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>

The GET method is not supported for this route. Supported methods: POST in laravel ...is there something i'm missing?

blade file :
<form action="{{URL::to('/admin/sender')}}"method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Controller:
public function notificationSender(Request $request)
{
$text= request()->text;
print_r($request->input());
event(new OrderComplete($text));
return view('admin.sender');
}
Route:
Route::post('/sender','HomeController#notificationSender');
the route is a subroute for a group..is there something i'm missing?
Give space between method and route parameters here
if your laravel version is 5 then use this
<form action="{{URL::to('/admin/sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Or else you can also name the route to make it easy to pass in action parameter of form like this.
Route::post('/sender','HomeController#notificationSender')->name('sender');
Then you can pass it in form like this
<form action="{{route('sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
For GET Method (Laravel 6)
blade file
<form method="GET" action="{{ route('sender') }}" enctype="multipart/form-data" >
#csrf
<input type="text" name="text">
<input type="submit">
</form>
Write Controller
public function notificationSender(Request $request)
{
$text= $request->get('text');
echo "<pre>"; print_r($text);
event(new OrderComplete($text));
return view('admin.sender');
}
Route
Route::get('sender','HomeController#notificationSender');

laravel Route::resource not working for delete

Laravel Route::resource('countries', 'CountriesController'); now working for DELETE
Working only as (separately)Route::delete('/countries/{country}/delete', 'CountriesController#destroy');
<div class="row">
<div class="col-12">
<h1>Details for {{ $country->countryName }}</h1>
<p>Edit</p>
<form action="/countries/{{ $country->id }}/delete" method="post">
<input name="_method" type="hidden" value="DELETE">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
</div>
</div>
not working, I'm stuck at url:
http://192.168.1.7:8000/countries/6/delete
saying '404|not found'
You don't need to append /delete in the URL.
Try this:
<form action="/countries/{{ $country->id }}" method="post">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
As mentioned by #nakov, you can also remove the hidden input field. The blade directive #method('DELETE') is sufficient to do the job.
Hope it helps!

Update method in html form in laravel

I'm trying to update inputs using html form in laravel:
<form action="{!! route('users.update',['id' => $users->id]) !!}" method="post">
<div class="form-group row">
<label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">customer_name</label>
<div class="col-sm-10">
<input value="{{$name}}" class="form-control form-control-lg" placeholder="col-form-label-lg">
</div>
<button type="submit" class="btn btn-primary btn-lg" > Edit</button>
</form>
Everything in the controller work perfectly however in the view page I received this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
No message
What am I doing wrong?
Please correct your route as POST like:
Route::post('update/{id}', 'YourController#update')->name('users.update');
You need to spoof the method as you are using to post the data. Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The #method Blade directive can create this field for you like this:
<form action="/foo/bar" method="POST">
#method('PUT') //add this to your form
</form>
or
<form action="/foo/bar" method="POST">
{{ method_field('patch')}} //add this to your form
</form>
You need to put #csrf and #method('PATCH') inside your form view.
I have the same problem and I had it worked after adding some lines in my code:
Just use $users->id instead of doing it as array ['id' => $users->id]
Use csrf and spoof the method by adding #method('PUT')
Your code should look like this:
<form action="{!! route('users.update', $users->id) !!}" method="post">
#csrf
<!--Some fields-->
#method('PUT')
</form>

Resources