MethodNotAllowedHttpException in RouteCollection.php line 218: Laravel - laravel

Im having trouble posting my form to my database using laravel. When i click submit, it shows me the error MethodNotAllowedHttpException in RouteCollection.php line 218. My HTML code is shown below. I have defined the routes as shown below and I have also pasted my PostController which contains the store function.
<div class="blog-page blog-content-2">
<div class="row">
<div class="col-lg-9">
<div class="blog-single-content bordered blog-container">
<div class="blog-comments">
<h3 class="sbold blog-comments-title">Leave A Comment</h3>
<form method="post" action="store">
<div class="form-group">
<input name="title" type="text" placeholder="Your Name" class="form-control c-square"> </div>
<div class="form-group">
<textarea name="body" rows="8" name="message" placeholder="Write comment here ..." class="form-control c-square"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn blue uppercase btn-md sbold btn-block">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
This is my route page
Route::resource('posts', 'PostController');
This is the PostController which contains the store function which is suppose to store the data into the database.
public function store(Request $request)
{
//Validate the data
$this->Validate($request, array(
'title'=>'required|max:255',
'body'=>'required'
));
//Store the data into the database
$post = new Post;
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
//redirect to another page
return redirect()->route('posts.show', $post->id);
}

The problem is here:
<form method="post" action="store">
You should put posts here:
<form method="post" action="posts">
You can see all routes created with Route::resource() by using php artisan route:list command. Here, you need to look at URI created for posts.store route.
Also, you need to add CSRF token to your form:
<form method="post" action="posts">
{{ csrf_field() }}

<form method="post" action="store"> will send you to the path store which you do not have, your form should post to the same url, like this:
<form method="post" action=".">

use
<form method="post" action="posts">

Related

a form with the PUT method is sent with the GET method

I have 2 controller with resource clients and sumsubs.
being on the clients.show route I created a form that I would like to send to the sumsubs.update endpoint but it does not work; the form still contacts sumsubs.show
here are the routes that I have defined
here is the form
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="PUT" action="{{ route('sumsubs.update', $client->id) }}">
#csrf
<div class="form-group row">
<div class="col-sm-10">
<input type="number" class="form-control" id="candidat" name="candidat" value="{{ $client->id }}" hidden required>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success float-right"> {{ __('Recheck') }} </button>
</div>
</form>
the following is my sumbsubs controller
public function show($id)
{
return 'not ok';
}
public function update(Request $request, $id)
{
return 'ok ';
}
this is what i get when i submit the form
I would like to know where is my mistake
I read the laravel documentation for form submission and tried with url and route methods but I still get the same result
You have to use method spoofing.
Change your method in form tag to Post:
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="POST" action="{{ route('sumsubs.update', $client->id) }}">
and add this after #csrf:
#method('PUT')

How to add form action to laravel function

I have a website I am currently editing tht was built with laravel. I have have a page that displays a "details of shipped package"
I added a form to page to update the current location of the shipped package on the details page.
<div class="row mb-30">
<div class="col-lg-12 mt-2">
<div class="card border--dark">
<h5 class="card-header bg--dark">#lang('Courier Location')</h5>
<div class="card-body">
<form action="{{route('....')}}" method="POST">
#csrf
<div class="modal-body">
<div class="form-group">
<label for="current_location" class="form-control-label font-weight-bold">#lang('Current Location')</label>
<input type="text" class="form-control form-control-lg" name="current_location" value="{{__($courierInfo->current_location)}}" required="">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn--primary"><i class="fa fa-fw fa-paper-plane"></i>#lang('Update')</button>
</div>
</form>
</div>
</div>
</div>
I have also added the update function in the controller
public function courierUpdate(Request $request, $id)
{
$request->validate([
'current_location' => 'required',
]);
$courierInfoUpdate =CourierInfo::findOrFail($id);
$courierInfoUpdate->current_location = $request->current_location;
$courierInfoUpdate->save();
$notify[] = ['success', 'Courier location info has been updated'];
return back()->withNotify($notify);
}
I am having problem with the laravel route to call that should be added as form action.
Declare a route on the web.php
Route::post('/courier-Update/{id}','App\Http\Controllers\YourControllerName#courierUpdate')->name('courier.Update');
and now just call this route in your form and also pass the id of that courier
like this:
route('courier.Update',$courier->id)
You can add a new route in routes/web.php
//import your controller at Beginning of the file
use App\Http\Controllers\YourController;
Route::post('update_location/{id}', [YourController::class, 'courierUpdate'])->name('updateLocation');
//or
Route::post('update_location/{id}', 'YourController#courierUpdate')->name('updateLocation');
And then in your blade view
<form action="{{ route('updateLocation', [ 'id' => $id]) }}" method="POST">
#csrf
</form>

PostsController#store not working when trying to create a post

This is my section and I want to send the form to the store function in the PostsController but when I hit submit it shows a 419 page expired however it should return to the posts page and the post should be stored in the database.
#section('content')
<form action="{{ action('PostsController#store') }}" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" id="title" name="title" placeholder="Title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea id="body" name="body" placeholder="Body"></textarea>
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
#endsection
and this is my store function in the PostsController
public function store(Request $request)
{
//create post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts');
}
The 419 page expired error generally comes from a csrf token issue.
Try adding #csrf within your form:
<form action="{{ action('PostsController#store') }}" method="POST">
#csrf
// etc
</form>

NotFoundHttpException in RouteCollection.php line 161 in laravel 5.3

none of solutions on net, didn't solve my problem.how can i correct this error?
form.blade.php:
<div class="row">
#include('admin.partials.errors')
<div class="col-xs-12 col-md-6">
<form action="{{ url('file') }}" name="POST" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="file_title">عنوان فایل:</label>
<input type="text" class="form-control" name="file_title" id="file_title" value="{{ old('file_title', isset($fileItem)? $fileItem->file_title:'') }}">
</div>
<div class="form-group">
<label for="file_description">توضیحات فایل:</label>
<textarea name="file_description" class="form-control" id="file_description" cols="30" rows="10" value="{{ old('file_description', isset($fileItem)? $fileItem->file_description:'') }}"></textarea>
</div>
<div class="form-group">
<label for="file_title">فایل:</label>
<input type="file" name="fileItem">
</div>
<div class="form-group">
<button class="btn btn-success" type="submit">ذخیره اطلاعات</button>
</div>
</form>
</div>
</div>
create.blade.php
#extends('layouts.admin')
#section('content')
#include('admin.file.form')
#endsection
FilesController.php
public function create()
{
return view('admin.file.create')->with('panel_title', 'ایجاد فایل جدید');
}
public function store(Request $request)
{
dd($request->all());
}
public function edit()
{
}
web.php
Route::get('/file','FilesController#index')->name('admin.file.list)//admin/user
Route::get('/file/create', 'FilesController#create')->name('admin.file.create');//admin/user
Route::post('/file/store','FilesController#store')->name('admin.file.store');//admin/file
Route::get('/file/edit/{file_id}','FilesController#edit')->name('admin.file.edit');//admin/file
Route::post('/file/edit/{file_id}','FilesController#update')->name('admin.file.update');//admin/file
});
when i click on save button to store file details on create file page in admin panel, i get this error!
i think you have to change this line
Route::post('/file/store','FilesController#store')->name('admin.file.store');//admin/file
to this
Route::post('/file','FilesController#store')->name('admin.file.store');//admin/file
Try changing
<form action="{{ url('file') }}" name="POST" method="post" enctype="multipart/form-data">
to
<form action="{{ url('file/store') }}" name="POST" method="post" enctype="multipart/form-data">
For Upload file on Laraval Public/Storage
Put this Code in Controller
FilesController.php
public function store(Request $request)
{ //after file store
return redirect(url('//**Put your Own url which you want**'));
}
Or Give another url which present in Your Web.php

How i can use Store() method to store image in database in laravel 5.2

when ever i fill submit form it give this massage BadMethodCallException in Macroable.php line 74:
Method store does not exist.
ArticleController
public function store(Request $request)
{
$file = $request->file('attach');
$filename = $file->store('local');
$article = new Article;
$article->title = $request->title;
$article->body = $request->body;
$article->attachment = $filename;
$article->save();
Session::flash('msg','Your data is saved now');
return back();
}
addarticle.blade.php
{{Session::get('msg')}}
<form class="container col-lg-6" action="article" method="post" enctype ="multipart/form-data">
{{csrf_field()}}
<div class="form-group"></div>
Title <input type="text" class="form-control" name="title">
Body <textarea name="body" id="" class="form-control" cols="30" rows="10">
</textarea>
<input type="file" name="attach">
<input type="submit">
</div>
Route
Route::get('/', function () {
return view('welcome');
});
Route::get('article','ArticleController#index');
Route::post('article','ArticleController#store');
Route::get('allarticle','ArticleController#show');
Change your opening form tag line to:
<form class="container col-lg-6" action="{{action('ArticleController#store')}}" method="post" enctype="multipart/form-data">
That will generate a url where the form will be submitted and will be less prone to errors. Because probably the error is that you are already on /article and action="article" generates url like /article/article.
PS: Your HTML is not valid according to "bootstrap's standards".
Change your form tag like this:-
<form class="container col-lg-6" action="{{ url('article') }}" method="post" enctype ="multipart/form-data">{{csrf_field()}}

Resources