Method not Allowed HTTP Exception - laravel

Here is the code of HTML Form
<form method="POST" action={{ route('store') }} class="col s12">
#csrf
<div class="row">
<div class="input-field col s6">
<input name="task" id="task" type="text" class="validate">
<label for="task">New Task</label>
</div>
</div>
#include('partials.coworkers')
<button type="submit" class="waves-effect waves-light btn">Add Task</button>
</form>
#isWorker
<br><br><br>
<form action="" class="col s6">
<div class="row">
<div class="input-field col s6">
<select>
<option value="" disabled selected>Send Invitation To</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label>Send Invitation</label>
</div>
</div>
<a class="waves-effect waves-light btn">Send Invitation</a>
</form>
Here is my code for store method inside a controller.
public function store(Request $request){
if ($request->input('task')) {
$task = new Task;
$task->content = $request->input('task');
Auth::user()->tasks()->save($task);
}
return redirect()->back();
}
web.php:
Route::middleware(['auth'])->group(function(){
Route::get('/', 'CrudApp#index');
Route::get('/store', 'CrudApp#store')->name('store');
Route::get('/edit/{id}', 'CrudApp#edit')->name('edit');
Route::get('/update/{id}', 'CrudApp#update')->name('update');
Route::get('/delete/id', 'CrudApp#delete')->name('delete');
});
I want to store the task into database. as soon as I hit save MethodNotAllowedHTTPException occurs and i am unable to figure out how to resolve the issue.

First of all:
php artisan route:cache
make it after any changes in route file (web.php in your case).
And now your code. Look u use POST:
<form method="POST" action={{ route('store') }} class="col s12">
so change:
Route::get('/store', 'CrudApp#store')->name('store');
on:
Route::post('/store', 'CrudApp#store')->name('store');

Using below command you will get the List of Routes where you can find your route:
php artisan route:list
Then as #Adam answer :
php artisan route:clear
php artisan route:cache
And in your query I think you are using Resource Controller.
If you are using resource method then you dont have to write all route separately
You can write your route in one line also:
Route::resource('demo-segment', 'DemoController');
You can also skip the unneccessory method from your resource controller
Route::resource('demo-segment', 'DemoController', [
'except' => ['show', 'edit', 'update', 'destroy'] // This is not usable methods
]);
Thanks, might help someone.

Related

Long link when I redirect to index

I have the following form:
<form action="/compras" method="POST">
#csrf
<div class="container">
<div class="row">
<div class="col-sm-6">
<select class="form-control" id="productor" name="productor">
<option value="0">Selecciona Productor</option>
#foreach ($productores as $productor)
<option value="{{$productor->id}}">{{$productor->nombre}}</option>
#endforeach
</select>
</div>
<div class="col-sm 6">
<select class="form-control" id="proveedor" name="proveedor">
<option value="0">Selecciona Proveedor</option>
#foreach ($proveedores as $proveedor)
<option value="{{$proveedor->id}}">{{$proveedor->nombre}}</option>
#endforeach
</select>
</div>
<button class="btn btn-secondary mr-2" name="action" value="search" type="submit">Buscar</button>
<button class="btn btn-secondary" name="action" value="create" type="submit">Crear</button>
</div>
</div>
</form>
I don't actually need to save data to the database in this controller, however I used the store function because of the post route that it includes. The idea is that if you press the Buscar button it will redirect to index filtering the table with the parameters, if you press Crear button it will redirect to a different controller and do something different
public function store(Request $request)
{
switch($request->input('action')){
case 'create':
return Redirect::action('ComprandoController#index', array('productor'=> $request->productor, 'proveedor'=>$request->proveedor));
break;
case 'search':
return Redirect::action('ComprasController#index', $request);
break;
}
}
The code is working as intended but when I redirect to index I have a problem in the link.
The link before filtering is: 127.0.0.1:4545/compras but after the filter it's something like
http://127.0.0.1:4545/compras?POST%20/compras%20HTTP/1.1%0D%0AAccept:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8%0D%0AAccept-Encoding:%20%20%20%20%20%20%20%20%20%20%20gzip,%20deflate%0D%0AAccept-Language:%20%20%20%20%20%20%20%20%20%20%20es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3%0D%0AConnection:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20keep-alive%0D%0AContent-Length:%20%20%20%20%20%20%20%20%20%20%20%2085%0D%0AContent-Type:%20%20%20%20%20%20%20%20%20%20%20%20%20%20application/x-www-form-urlencoded%0D%0ACookie:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20XSRF-TOKEN=eyJpdiI6IlhvQkVGa2libnlydUcyRDNQVElRdlE9PSIsInZhbHVlIjoiZWh0d091RUphUE9BV0t0dUtjY2w0akw4VEx2TjkxaVIySXFKWmpJbkVueVU5cHpHZXgvQ3l0TGc0U28zNFAwNSIsIm1hYyI6IjEzMDk3NTI5MWRjOTI2Nzg3YjA4ZWNiYTVlODZjN2ZjMGJjMWFjNWRkNjRjNjcwNDYwOWI5YjBjODE3NmRkMWUifQ%3D%3D;%20laravel_session=eyJpdiI6IkFCcGR2d01xZmwwbmh4dGprMGNYZHc9PSIsInZhbHVlIjoiVHI1aGUzWjVTdlgyVVE3WmFGV01PSjhTb01ZTWxnVllEL2Q2SmVxQ1BpVW5qY24yZjlLc01pdUVRNjNoRDEyTiIsIm1hYyI6ImFkNDI3NzBjZmUwZmRmZmE5ZDM2NjM1YTBkMWQyZjM2ZTUwOTQ4NjcwNzlhNGJkNzJhYjFlNTI1NjE5OTZjOWIifQ%3D%3D%0D%0AHost:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20127.0.0.1:4545%0D%0AOrigin:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545%0D%0AReferer:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545/compras?POST%20/compras%20HTTP/1.1%0D%0AAccept:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8%0D%0AAccept-Encoding:%20%20%20%20%20%20%20%20%20%20%20gzip,%20deflate%0D%0AAccept-Language:%20%20%20%20%20%20%20%20%20%20%20es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3%0D%0AConnection:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20keep-alive%0D%0AContent-Length:%20%20%20%20%20%20%20%20%20%20%20%2085%0D%0AContent-Type:%20%20%20%20%20%20%20%20%20%20%20%20%20%20application/x-www-form-urlencoded%0D%0ACookie:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20XSRF-TOKEN=eyJpdiI6IkpNVTA3dDk1cDZlSUhYNUorRndkamc9PSIsInZhbHVlIjoiK1JXTVFyN2dFcHJLaC9uK1QzMjhjQ1FTa2ZuQzFCM1FabnpiY3lXbzZPMzdVbGE2c0w2b3dWSlp5YnpMdVc0VyIsIm1hYyI6IjRhOTdjNTJiMTA1ODI2ODRmMDVhOTgyMDg1NTMxMzY1YzhiNTI2MGJhMzk1YzZjMzBhNTg5YmJhNDY0N2YzZDYifQ%3D%3D;%20laravel_session=eyJpdiI6ImU4bUUxenZUR0RCaTBJZis4WTg4TGc9PSIsInZhbHVlIjoiYkxucWdXNi81YmlvNzRUbjlqSEpxdUt3ZnBiazVxYURCaVBXSFYwbXZPU0hxTk1Sa2RFSDJVTkIyZUdHVTNGeSIsIm1hYyI6ImFmYWVjMjM1NmMwYTljMjkzNGQ4MmFlYjIwY2Q3MzJhMzI1OTY4MjUwM2ZkZmRhZmIyNTBjNmJiZjYxZDQ1NWQifQ%3D%3D%0D%0AHost:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20127.0.0.1:4545%0D%0AOrigin:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545%0D%0AReferer:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://127.0.0.1:4545/%0D%0AUpgrade-Insecure-Requests:%201%0D%0AUser-Agent:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Mozilla/5.0%20%28Windows%20NT%2010.0;%20Win64;%20x64;%20rv:80.0%29%20Gecko/20100101%20Firefox/80.0%0D%0ACookie:%20XSRF-TOKEN=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5;%20laravel_session=eV6iavmA0PGGAkisR1egs2mLHqbW6w9j58maOxDC%0D%0A%0D%0A_token=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5&productor=0&proveedor=0&action=search%0D%0AUpgrade-Insecure-Requests:%201%0D%0AUser-Agent:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Mozilla/5.0%20%28Windows%20NT%2010.0;%20Win64;%20x64;%20rv:80.0%29%20Gecko/20100101%20Firefox/80.0%0D%0ACookie:%20XSRF-TOKEN=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5;%20laravel_session=eV6iavmA0PGGAkisR1egs2mLHqbW6w9j58maOxDC%0D%0A%0D%0A_token=fBi5Ch1lKRYNMLswqGIkUc94OOTjNTa8aNMmZ7h5&productor=1&proveedor=3&action=search
How could I get rid of the grotesque big link without ruining the working code?
Can you add your route file ?
I prefer use return redirect()->route('route.name', ['id' => 1]); to do redirect from controller.
https://laravel.com/docs/7.x/redirects#redirecting-named-routes
Also, you can use $request->all() or $request->only(['productor', 'proveedor']).
PS : the link is long, because Laravel serialize all datas from $request.

laravel post from view does not call controller method

I am trying to send a post request from my view but somehow this request is not passing to my controller method while other controller methods are working..
my routes:
Route::get('/executes', 'ExecuteController#index')->name('execute.index');
Route::post('/executes', 'ExecuteController#store')->name('execute.store');
Route::get('/executes/create', 'ExecuteController#create')->name('execute.create');
my view:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-6">
<h2>Create Execute</h2>
<form method="post" action="/executes" enctype="multipart/form-data" class="pt-4">
#csrf
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name">
#error('name')
<p class="pt-3 text-danger">
{{ $message }}
</p>
#enderror
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="map">Map</label>
</div>
<select class="custom-select" id="map" name="map">
<option selected>Choose...</option>
#foreach($maps as $map)
<option value="{{ $map->id }}">{{ $map->name }}</option>
#endforeach
</select>
#error('map')
<p class="pt-3 text-danger">
{{ $message }}
</p>
#enderror
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
#endsection
my controller:
class ExecuteController extends Controller
{
public function create()
{
return view('execute/create', ['maps' => Map::all()]);
}
public function store()
{
// is not even getting here
dd('test');
}
}
any ideas why my method is not getting called?
Could you try to change this
FROM
action="/executes"
TO
action="{{route('/executes')}}"
OR
action="{{url('/executes')}}"
Hope it helps
You're using named routes, so refer to their names when building a URL instead.
Example:
action={{url('execute.store')}}
Please use {{ url('ROUTE_NAME')}} in action or where you want to give link
Use route in the form action as following
action="{{route('execute.store')}}"
guys thank you for your replies but the problem was on the frontend, I had a js included outside the html tag in my layout, somehow that messed up my store method, by fixing that it now works

Laravel Update Function Not Running Correct Response

When I attempt to update a record in my Laravel application, it is running the wrong URL causing an error 404. This function was working fine when I was developing locally however now it is hosted on a one.com server, it has stopped working.
edit.blade.php
<form method="POST" action="gins/{{ $gins->id }}">
#method('PATCH')
#csrf
<div class="field">
<label class="label" for="gin">Gin</label>
<div class="control">
<input type="text" class="input" name="gin"
placeholder="Gin" value="{{ $gins->gin }}">
</div>
</div>
<div class="field">
<label class="label" for="size">Bottle Size(ml)</label>
<div class="control">
<input type="text" class="input" name="size"
placeholder="Size (ml)" value="{{ $gins->size }}">
</div>
</div>
<div class="field">
<label class="label" for="price">Price(£)</label>
<div class="control">
<input type="text" class="input" name="price"
placeholder="Price of Gin" value="{{ $gins->price }}">
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-success">Update Record
</button>
</div>
</div>
</form>
Route
Route::patch('gins/{gin}', 'PostsController#update')->middleware('auth');
Auth::routes();
Controller
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
return redirect('gins');
}
The URL for the edit page is Laravel/gins/7/edit. When I click the submit button it's returning the URL Laravel/gins/7/gins/7 when it should be redirecting back to Laravel/gins/7.
The 7 in the Url is the record id from the particular record I'm attempting to update.
It's always a bad idea to hardcode urls like that. The following
<form method="POST" action="gins/{{ $gins->id }}">
in a route like laravel/gins/ would evaluate to laravel/gins/gins/7.
Also, routes change all the time in a dynamic web application. For this reason, I'd suggest you to use Named Routes.
For example:
Route::patch('gins/{gin}', 'PostsController#update')
->middleware('auth')
->name('posts.update');
and then change your form action to this:
<form method="POST" action="{{ route('posts.update', ['gin' => $gins->id]) }}">
I would also clean up your update() method a bit.
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
// change this to a named route as well
return redirect('gins');
// or if you just want to return back to the previous page, you can do
// return back();
}

Error in pagination when going on next page

I am working on a project with pagination in it. When a user try to search records it will be shown using pagination. Records on first page are shown perfectly without any error but when I go to next page it gives me error
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
Can anyone please help me to sort this problem. I am sharing my code below.
Controller:
public function index(Request $request)
{
$hospitals = DB::table('hospitals')
->where('city',$request->city)
->paginate(6);
return View::make('/patientPanel/patientHospitalList')->with('hospitals',$hospitals);
}
Route:
Route::post('patientPanel/patientHospitalList',
'patientHospitalListController#index')->name('hospitalList');
View from where city is selected:
<form class="form-horizontal" role="form" method="POST" action="{{ route('hospitalList')}}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">City</label>
<div class="col-md-6">
<select class="form-control" name="city">
<option value="lahore">Lahore</option>
<option value="Faislabad">Faislabad</option>
<option value="karachi">Karachi</option>
<option value="islamabad">Islamabad</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary" style="margin-bottom: 50px; margin-left: 250px;">
Submit
</button>
</div>
</div>
</form>
And the view where records are shown
<section id="team">
#include('/patientPanel/load')
</section>
Please tell me how to solve this probelm so that when user goes no next page records will be shown.
Your route should be using the "get" method:
Route::get('patientPanel/patientHospitalList',
'patientHospitalListController#index')->name('hospitalList');
You'll need to change the method on your form to "get" as well.

MethodNotAllowedHttpException in RouteCollection.php line 218: 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">

Resources