MethodNotAllowedHttpException in RouteCollection.php line 218 - php

i'm new to laravel, and i found few decent tutorials to help me understand and get started with it.
the problem is-> whenever i want to use the post method this exception raises MethodNotAllowedHttpExceptionbut unlike, maybe 99% of who asked similar questions, in my case it says the exception is in RouteCollection.php line 218, which is unusual but not to laravel 5.2.x
the following is the methode post in routes.php:
Route::post('/ajouter_produit',
[
'uses'=>'ProductController#addProduct',
'as'=>'ajouter_produit',
]);
i even tried adding this method to a middleware route group but the problem remained.
this is my controller:
public function addProduct (Request $request)
{
$this->validate($request, [
'label'=>'required|alpha',
'prix'=>'required|numeric',
]);
$prod = new Product();
$prod->label=$request['label'];
$prod->type=$request['type'];
$prod->prix=$request['prix'];
$prod->save();
return view('welcome');
}
and this is my form:
<form action="{{ route('ajouter_produit') }}" method="post" >
<input type="text" name="label" id="label"/>
<select name="type" id="type">
<option value="1">Par unité</option>
<option value="2" selected>Par kilo</option>
</select>
<input type="text" name="prix" id="prix"/>
<button type="submit">Ajouter</button>
<input type="hidden" value="{{ Session::token() }}" name="_token"/>
i also tried this but it raised the same problem:
Route::post('/trypost', function () {
return 'hello post';
});
can you please help me !!
if you need any other source just ask for it.
Every effort will be much appreciated. thank you

take note that if you are using route(), it is expecting route name, such as user.store or user.update.
so my suggestion is, try use url() for your open form
<form action="{{ url('ajouter_produit') }}" method="post" >
more details on laravel docs

"#mydo47: Missing method get. First you should create route with method get return view. Next, in view page you call method post validate and save to your model." this solved it

Related

Laravel 5.8: The POST method is not supported for this route

I have a form at the Blade like this:
#forelse($orders as $order)
<form method="POST" action="{{ route('orders.newprint', ['id'=>$order->id]) }}">
#csrf
<tr>
<td><input class="form-check-input" name="orderCheck[]" type="checkbox" value="{{ $order->ord_id }}"> </td>
<td>{{ $order->ord_id }}</td>
</tr>
#empty
<td colspan="7" class="text-center">Nothing to show</td>
#endforelse
<hr>
<label>Actions:</label>
<select class="form-control select2" name="actions" id="actions">
<option value="">Select...</option>
<option value="print_factors">Print Factors</option>
<option value="print_orders">Print Orders</option>
</select>
<button class="btn btn-primary float-left">Do</button>
</form>
And I added this route:
Route::post('orders/newprint/{order}','OrdersController#prnpriview')->name('orders.newprint')->middleware('permission:shop-manage');
And the method of Controller goes here:
public function prnpriview(Request $request, Order $order)
{
$checks = $request->input('orderCheck');
foreach($checks as $check){
dd($check);
}
}
But I get this error:
The POST method is not supported for this route. Supported methods: GET, HEAD, DELETE.
I don't know why it shows this because I have specified POST as the route action!
So if you know how to solve this, please let me know...
Thanks in advance.
This problem comes when you have another route that either begins like that one, for example:
Route::post('orders/newprint/...
or it has the same name
->name('orders.newprint')
And it's defined after the wanted route.
Clarification: In Laravel, when there are same route names defined only the last one will be taken... It works as the array key principle... If you push same key into existing array, that what you set last will be the one in use:
$array = [
'name' => 'route_name'
];
$array['name'] = 'some_different_name';
print_r($array);
// result
Array
(
[name] => some_different_name
)
The pattern to match should begin with a forward slash: '/orders/newprint/{id}'
While I'd rather call it: '/orders/{id}/print' (a matter of taste).
In your action prnpriview() in Controller, You put Order object in parameter. And you are sending order id in form
change
public function prnpriview(Request $request, Order $order)
to
public function prnpriview(Request $request, $order_id)
May be issue is in your routing methods.
Try out below things.
<form method="POST" action="{{ route('orders.newprintv1', $order->id) }}">
Mention id directly in action route we didn't required a array for single record.
In Route :
Route::post('orders/newprint/{order}','OrdersController#prnpriview')->name('orders.newprintv1')->middleware('permission:shop-manage');
Change name of the route orders.newprint make sure any other route name is not like the same above you write route in route.php check all the routes before this one.
may be check for the resource route ? define any route with orders/newprint.
due to that also making issue.
one more thing for confirm move your existing route without making any changes
make it to first at top of the route.php file like this is your first route.
and then check it will works then other route making conflict with same name.
Use this way
<form method="POST" action="{{ route('orders.newprint', $order->id) }}">

Laravel 5.2 Form update error

Okay so guys i've a problem when working with laravel's update & i've spent 3 days and no one can solve my problem too so i decide to ask you all pro coder, please kindly help me
First, this is part of my View (editkeluhan.blade.php)
<form class="form-horizontal" role="form" method="POST" action="{{ url('/editkeluhanadmin/{$keluhan->id}') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<input name="_method" type="hidden" value="PATCH">
</div>
Second, This is my Route
Route::get('editkeluhan/{id}','AdminController#editkeluhan');
Route::post('editkeluhanadmin/{id}', 'AdminController#updatekeluhanadmin');
public function editkeluhan($id){
$halaman="tindaklayanan";
$keluhan=keluhan::findOrFail($id);
return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
}
public function updatekeluhanadmin(Keluhan $keluhan, Request $r){
$halaman = 'tindaklayanan';
$keluhan->update($r->all());
return redirect('/');
Third, this is AdminController
public function editkeluhan($id){
$halaman="tindaklayanan";
$keluhan=keluhan::findOrFail($id);
return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
}
public function updatekeluhanadmin(Keluhan $keluhan, Request $r){
$halaman = 'tindaklayanan';
$keluhan->update($r->all());
return redirect('/');
}
This is ERROR, but when i change my Route into this
Route::resource('editkeluhanadmin', 'AdminController#updatekeluhanadmin');
Error is gone BUT Its not update in database
Please Please Help me
You use PATCH action in your form
<input name="_method" type="hidden" value="PATCH">
so you need to change your route to:
Route::patch('editkeluhanadmin/{id}', 'AdminController#updatekeluhanadmin');
Visit https://laracasts.com/discuss/channels/laravel/update-form-data-laravel-52 to see my disscussion there & the answer if anyone need, good luck.

Php laravel 5.3 passing an input value from one blade file to another blade file

I want to pass an input value from one blade file to another blade file.
I'm new to PHP Laravel, and I'm getting an error when attempting to use it.
I think my syntax is wrong here. Can somebody help?
channeling.blade:
<select class="form-control " name="fee" id ="fee"></select>
This is the link to the next page, where i want to send the value of "fee":
<input type="hidden" value="fee" name="fee" />
Click to Channel</p>
This is my web.php:
Route::post('pay', [
'as' => 'fee',
'uses' => 'channelController#displayForm'
]);
This my controller class:
public function displayForm()
{
$input = Input::get();
$fee = $input['fee'];
return view('pay', ['fee' => $fee]);
}
Error message:
Undefined variable: fee
(View: C:\xampp\htdocs\lara_test\resources\views\pay.blade.php)
pay.blade:
<h4>Your Channeling Fee Rs:"{{$fee}}"</h4>
You should use form to send post request, since a href will send get. So, remove the link and use form. If you use Laravel Collective, you can do this:
{!! Form::open(['url' => 'pay']) !!}
{!! Form::hidden('fee', 'fee') !!}
{!! Form::submit() !!}
{!! Form::close() !!}
You can value inside a controller or a view with request()->fee.
Or you can do this:
public function displayForm(Request $request)
{
return view('pay', ['fee' => $request->fee]);
}
I think you can try this, You mistaken url('pay ') with blank:
change your code:
Click to Channel</p>
to
Click to Channel</p>
Further your question require more correction so I think you need to review it first.
You can review about how to build a form with laravel 5.3. Hope this helps you.
You have to use form to post data and then you have to submit the form on click event
<form id="form" action="{{ url('pay') }}" method="POST" style="display: none;">
{{ csrf_field() }}
<input type="hidden" value="fee" name="fee" />
</form>
On the click event of <a>
<a href="{{ url('/pay') }}" onclick="event.preventDefault();
document.getElementById('form').submit();">
Logout
</a>
tl;dr: I believe #AlexeyMezenin's answer is the best help, so far.
Your current issues:
If you have decided to use Click to Channel, you should use Route::get(...). Use Route::post(...) for requests submitted by Forms.
There isn't an Input instance created. Input::get() needs a Form request to exist. Thus, the $fee an Undefined variable error message.
The value of <input type="hidden" value="fee" name="fee"/> is always going to be the string "fee". (Unless there's some magical spell casted by some JavaScript code).
The laravel docs suggest that you type-hint the Request class when accessing HTTP requests, so that the incoming request is automatically injected into your controller method. Now you can $request->fee. Awesome, right?
The way forward:
The BasicTaskList Laravel 5.2 tutorial kick-started my Laravel journey.
I changed the code like this and it worked..
echanneling.blade
<input type="hidden" value="fee" name="fee" />
<button type="submit" class="btn btn-submit">Submit</button>
channelController.php
public function about(Request $request)
{
$input = Input::get();
$fee = $input['fee'];
return view('pay')->with('fee',$fee);
}
Web.php
Route::post('/pay', 'channelController#about' );

Laravel 5.1 Creating default object from empty value

I am using Laravel 5.1 PHP framework. When I try to update my record, I get the error:
"ErrorException in AdminController.php line 108: Creating default
object from empty value".
I have searched in google but I can't find any results to solve my problem.
Routes
Route::get('/admin/no', 'AdminController#index');
Route::get('/admin/product/destroy/{id}', 'AdminController#destroy');
Route::get('/admin/new', 'AdminController#newProduct');
Route::post('/admin/product/save', 'AdminController#add');
Route::get('/admin/{id}/edit', 'AdminController#edit');
Route::patch('/admin/product/update/{id}', 'AdminController#update')
AdminController
public function edit($id)
{
$product = Product::find($id);
return view('admin.edit', compact('product'));
}
public function update(Request $request, $id)
{
$product = Product::find($id);
$product->id = Request::input('id');
$product->name = Request::input('name');
$product->description = Request::input('description');
$product->price = Request::input('price');
$product->imageurl = Request::input('imageurl');
$product->save();
//return redirect('/admin/nο');
}
enter code here
edit.blade.php
div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">Edit Product</div>
</div>
<div class="panel-body" >
<form action="/admin/product/update/{id}" method="POST"><input type="hidden" name="_method" value="PATCH"> <input type="hidden" name="_token" value="{{ csrf_token() }}">
enter code here
The problem is that $product = Product::find($id); returns NULL. Add the check:
if(!is_null($product) {
//redirect or show an error message
}
Though this is your update method, so probably you're having an error while building the url for this method. It might be a wrong id you're passing to this route.
Your form action has an error:
<form action="/admin/product/update/{id}" method="POST">
Notice the curly braces, Blade's syntax is {{ expression }}, not just {}. So id is never passed to the product.update route. Just change it to:
<form action="/admin/product/update/{{$id}}" method="POST">
check if the product exists then do the update
The form will look like this
<form action="/admin/product/update/{{$id}}" method="POST">
$ sign was missing :)
For update entity in laravel uses PUT method not POST. update form method and try.
<form action="/admin/product/update/{id}">
<input name="_method" type="hidden" value="PUT">
Check your web.php file maybe mistake your controller name.
This may be helpful for similar issues for others..
Instead of $product = Product::find($id); use $product = Product::where('some_other_id', $id);. I am saying this because you might be giving reference to some_other_id like a foreign key instead of primary key.

How to get data transfer from the form into database Laravel 5

I'm trying to transfer data from the form to overwrite the content database .But get the error.
It is my route,method and form.
Route::get('edit/{id}','JokeController#edit');
public function edit(JokeModerateRequest $request,$id) {
$joke = Joke::findOrFail($id);
return view('jokes.edit', [ 'joke' => $joke ]);
}
<form action="/update" method="post">
<input type="text" value="{{ $joke -> content}}" name="body">
<input type="submit" value="Save">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
But when I try use next route and method
Route::post('update/{id}','JokeController#update');
function update(JokeModerateRequest $request,$id)
$joke = Joke::findOrFail($id);
$joke->content = $request -> get('content');
$joke->save();
return back();
I have next error
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 145:
The problem lies here:
<form action="/update" method="post">
This will redirect to /update route (which you haven't defined) instead of /update/id. Those two are different routes.
To fix this, change action of the form to:
<form action="/update/{{ $joke->id }}" method="post">

Categories