Laravel methodNotAllowed on post - php

I'm not sure why it's not working...
I'm trying to update a user and I keep getting method not allowed error exception.
-- routes
Route::get('superadmin/users', ['as' => 'superadmin.users', 'uses' => 'SuperAdminController#usersIndex']);
Route::post('superadmin/users/{id}', ['as' => 'superadmin.editUser', 'uses' => 'SuperAdminController#editUser']);
-- controller
public function usersIndex()
{
$users = User::all();
return View::make('superadmin.users',compact('users'));
}
public function editUser($id)
{
$user = User::findOrFail($id);
$user->email = Input::get('email');
$user->save();
return Redirect::route('superadmin.users')->with('alertsuccess', 'User has been updated.');
}
-- view
{{ Form::model($user, ['method' => 'PATCH', 'route' => ['superadmin.editUser', $user->id], 'class' => 'form']) }}
<div class="form-group">
{{ Form::label('email', 'Email:', ['class' => 'placeholder-hidden']) }}
{{ Form::text('email', Input::old('email'), ['class' => 'form-control']) }}
</div>
{{ Form::submit('Update User', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}

This is most probably as you need to set up a resource controller in order to use the PATCH method. Try using POST instead.

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']) !!}

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 destroy and update methods not working

Trying to delete entries using the destroy method in Laravel controller.
public function destroy($id)
{
$university = University::find($id);
$university->delete();
return redirect('/universities');
}
And this is what i'm using in the view
{!!Form::open(['action' => ['UniversityController#destroy', $university->Id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
Getting no errors and browser redirects after the button is activated as instructed, but the entry still remains in the veiw list and in the DB. Using MySQL.
Posting to the DB also works fine, but having same problems with update method. No errors and get redirected as I should but no update has happened.
public function update(Request $request, $id)
{
$this->validate($request, [
'Name' => 'required',
'Country' => 'required'
]);
$university = University::find($id);
$university->Name = $request->input('Name');
$university->Country = $request->input('Country');
$university->save();
return redirect('/universities');
}
And in view:
{!! Form::open(['action' => ['UniversityController#update', $university->Id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('Name', 'Name')}}
{{Form::text('Name', $university->Name, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
<div class="form-group">
{{Form::label('Country', 'Country')}}
{{Form::text('Country', $university->Country, ['class' => 'form-control', 'placeholder' => 'Country'])}}
</div>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Submit', ['class' =>'btn btn-primary'])}}
{!! Form::close() !!}
Also tried running without the hidden form methods, but same result.
My routes:
Route::get('/universities', 'UniversityController#index');
Route::get('/universities/create', 'UniversityController#create');
Route::get('/universities/{id}/edit', 'UniversityController#edit');
Route::put('/universities/{id}', 'UniversityController#update');
Route::post('/universities/create', 'UniversityController#store');
Route::delete('/universities/{id}', 'UniversityController#destroy');
Solved by setting public $primaryKey = 'Id'; in the model.

How to get Data from previous page in Laravel

So, i have some categories. And in each category you can add posts.
But in form page for adding posts, how to get the value of that category i.e. of previous page?
This is my form:
<div class="container">
{!! Form::open(['action' => 'TopicController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title of the Post']) }}
</div>
<div class="form-group">
{{ Form::label('desc', 'Desc') }}
{{ Form::textarea('desc', '', ['class' => 'form-control', 'placeholder' => 'Description of the Post']) }}
</div>
{{ Form::submit('Submit', ['class' => 'btn btn-default']) }}
{!! Form::close() !!}
</div>
Link in category page to form:
Create New Post
Controller:
$this->validate($request, [
'title' => 'required',
'desc' => 'required',
])
$topic = new Topic;
$topic->topic_title = $request->input('title');
$topic->topic_body = $request->input('desc');
$topic->user_id = auth()->user()->id;
$topic->save();
return redirect('/')->with('Seccess', 'Topic Created');
show.blade.php contains link to this form page. But to get the id of the category page that is referring to this form??
You need to pass category_id into your link as route parameter:
Create New Post
Catch category_id in /topics/create/category_id route:
Route::post('/topics/create/{category}', 'TopicsController#create');
And then use it to create a hidden field in your form:
<div class="container">
{!! Form::open(['action' => 'TopicController#store', 'method' => 'POST']) !!}
{{ Form::hidden('category_id', $category_id) }}
...
</div>
And then in your controller:
...
$topic->category_id = $request->input('category_id');
$topic->user_id = auth()->user()->id;
$topic->save();
...

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