MethodNotAllowedHttpException laravel 5 - php

Why am I getting this error?
Routes:
Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
Route::get('/',['uses'=>'Admin\IndexController#index','as'=>'adminIndex']);
Route::resource('/products','Admin\ProductController');
});
Form:
{!! Form::open(['url' => route('admin.products.edit',['products'=>$product->id]),'class'=>'form-horizontal','method'=>'POST']) !!}
{{ method_field('EDIT')}}
{!! Form::button('Edit', ['id'=>'submit','type'=>'submit']) !!}
{!! Form::close() !!}
Also, when I'm trying to get list of routes by typing php artisan route:list, I'm getting error:
[Symfony\Component\HttpKernel\Exception\HttpException]
What's the problem?

I think your resource under admin prefix does not take the admin prefix. So you still have to use it without admin prefix. Besides, you are using 'url' instead of 'route'.
{!! Form::open(['url' => '/admin/products/'.$product->id.'/edit'),'class'=>'form-horizontal','method'=>'POST']) !!}
If you want to keep using route
{!! Form::open(['route' => ['products.edit', 'products'=>$product->id], 'class'=>'form-horizontal','method'=>'POST']) !!}
If you are tryint to update instead
{!! Form::model($product, ['method' => 'PATCH', 'route' => ['products.update', $product->id], 'class' => 'form-horizontal' ]) !!}

Since edit method uses GET, change your code to:
{!! Form::open(['route' => ['admin.products.edit', $product->id], 'class' => 'form-horizontal', 'method' => 'GET']) !!}
{!! Form::submit('Edit', ['id' => 'submit']) !!}
{!! Form::close() !!}
Also, since it's GET, you can use simple link:
<a href="{{ route('admin.products.edit', $product->id) }}">
<button class="btn" id="submit">Edit</button>
</a>

look down should be use route 'as'=>'admin.products.edit' and get id also redirect to controller function edit products
Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
Route::get('/edit/{id}/product',
[
'uses'=>'Admin\IndexController#index',
'as'=>'admin.products.edit'
]);
Route::resource('/products','Admin\ProductController');
});

Related

laravel form submit uses wrong controller method

Im trying to use a form to submit reviews for products but I believe the submit button uses the incorrect controller store method. I have a controller for products and one for reviews. The products store works correctly and I can see the database being populated once submitted however when I go to submit a review for a product it will throw the custom error messages from the product store form. If I change the reviews form::open to the products form::open it will throw an error: The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Products form (works properly)
{!! Form::open(['action' => 'App\Http\Controllers\ProductsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
... labels and text ...
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
Reviews form
<div>
<p>Write a review</p>
<!-- submit review form -->
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
<div class="form-group">
{{ Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Write your message']) }}
</div>
<div class="form-group">
{{ Form::label('rating', 'Rating') }}
{{ Form::select('rating', ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'], '1') }}
</div>
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
</div>
ReviewsController store
public function store(Request $request, $id)
{
$this->validate($request, ['description' => 'nullable',
'rating' => 'nullable',
]);
$review = new Review;
$review->rating = $request->input('rating');
$review->reviewerid = auth()->user()->id;
$review->productid = $id;
$review->description = $request->input('description');
$review->save();
return redirect('/products/$id')->with('success', 'Review submitted');
}
Web.php file
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#abouts');
Route::get('/cart', 'App\Http\Controllers\PagesController#cart');
Route::get('/checkout', 'App\Http\Controllers\PagesController#checkout');
Route::get('/dashboard', 'App\Http\Controllers\PagesController#services');
Route::get('/categories/{Category}', 'App\Http\Controllers\PagesController#category');
Route::resource('reviews', 'App\Http\Controllers\ReviewsController');
Route::resource('products', 'App\Http\Controllers\ProductsController');
Auth::routes();
The error is because of this line:
Form::hidden('_method', 'PUT')
You're telling laravel to use put method. Delete it and it will work fine.
For your form action, I think you have type in this line:
{!! Form::open(['reviews' => 'App\Http\Controllers\ReviewsController#store']) !!}
Change it to :
{!! Form::open(['action' => 'App\Http\Controllers\ReviewsController#store']) !!}

My laravel collective form is not submitting when I click submit

I have a form in my blade template when I click submit button nothing is happing it is not going to my controller.
this is the form
{{Form::open(['action' => ['HomeController#addcity'], 'method' => 'POST']) }}
{!! Form::select('city_add', array("CAM" => "CAM","KL" => "KL","IPOH" => "IPOH"), 'S',['style'=>'
}'],['class' => 'form-control','placeholder'=>'hotel_name']); !!}
{{Form::text('city_add',"CAM") }}
{{Form::submit('Submit',['class'=>'btn btn-danger'])}}
{!! Form::close() !!}
this is my route
Route::post('/home', 'HomeController#addcity');
My route controller
Your form select element like below:
You should not use same name for the select tag and input tag.
{{Form::open(['action' => ['HomeController#addcity'], 'method' => 'POST']) }}
{!! Form::select('city_option', array("CAM" => "CAM","KL" => "KL","IPOH" => "IPOH"),['class' => 'form-control','placeholder'=>'hotel_name']); !!}
{{Form::text('city_add',"CAM") }}
{{Form::submit('Submit',['class'=>'btn btn-danger'])}}
{!! Form::close() !!}
And In controller( for the test )
public function addcity(Request $request )
{
echo '<pre>';
print_r($request->all());
exit();
}
if you would like to add style in form element use below code:
{{Form::open(['action' => ['HomeController#addcity'], 'method' => 'POST']) }}
{!! Form::select('city_option', array("CAM" => "CAM","KL" => "KL","IPOH" => "IPOH"),'S',['style'=>'color:red'],['class' => 'form-control','placeholder'=>'hotel_name']); !!}
{{Form::text('city_add',"CAM") }}
{{Form::submit('Submit',['class'=>'btn btn-danger'])}}
{!! Form::close() !!}

Cannot bind models in components

I define a component for my forms :
/resources/views/components/form.blade.php
{!! Form::model($model, $form) !!}
{{ $slot }}
{!! Form::close() !!}
and in the edit blade file I write some codes like this :
#component('components.form',[
'model' => $user,
'form' => [
'route' => ['users.update', $user],
'method' => 'put',
'class' => 'col-sm-12'
]
])
{!! Form::text('username', null, ['class' => 'form-control']) !!}
#endcomponent
Unfortunately, the fields cannot fill with previous values.
But when I {{ dump($model) }} in /resources/views/components/form.blade.php the variable have a Object value.
What is the problem?

Laravel can not upload files with id to database

I want to upload file with user id to database, but I have a trouble.
NotFoundHttpException
Here's my code:
{!! Form::open(array('url' => '/uploaded', $client->id, 'files' => true)) !!}
{!!Form::file('image') !!}
{!!Form::submit('Upload File') !!}
{!! Form::close() !!}
and web.php
Route::post('uploaded/{id}/', 'FileController#uploaded');
Your url should be
Form::open(array('url' => '/uploaded/'.$client->id,'files' => true)) !!}
Then only that route will work, which you should have defined in web.

Laravel 5, nothing displayed when form submit button is clicked

I am using Laravel 5.3, the task that i am currently working on is the Form routing.
This is my routes.php file.
Route::group(['middleware' => 'web'], function() {
Route::get('/login', ['as' => 'login', 'uses' => 'LoginController#login']);
Route::post('/handleLogin', ['as' => 'handleLogin', 'uses' => 'LoginController#handleLogin']);
});
The actual Form code in the view.
{!! Form::open(array('route' => 'handleLogin')) !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', array('class' => 'form-control')) !!}
</div>
{!! Form::token() !!}
{!! Form::submit('Login', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
The controller that has the handle function.
/* handleLogin function to request the data*/
public function handleLogin(Request $request){
$data = $request-> only('email', 'password');
if(\Auth::attempt($data)){
return 'Is Logged In';
return redirect()-> intended('/home');
}
return back()->withInput();
}
When i click on Login button, a blank page is displayed instead of the page that would display 'Is Logged In'.
Any help would be appreciated.
You should remove web middleware from routes file since you're using Laravel 5.3 in which web middleware is added automatically. Adding it manually will cause problems.

Categories