MethodNotAllowedHttpException on resource defined method Laravel-4 - php

I created a very simple form so that I could use a submit button rather than a link to open up an edit users page. Using a link works perfectly, but the form button fails and yields a MethodNotAllowedHttpException even though the method ("edit") is perfectly defined in the UsersController resource and otherwise works fine.
Route:
Route::resource('users','UsersController');
UsersController:
public function edit($id)
{
$user = $this->user->find($id);
return View::make('users.edit')->with('user',$user);
}
show.blade.php:
<!-- This works fine: -->
{{ link_to_route('users.edit', ("Edit: " .$user->first_name." ".$user->last_name), $user->id) }}
<!-- This doesn't work, and yields the Method Not Allowed exception: -->
{{ Form::open(array('route' => array('users.edit',$user->id))) }}
{{ Form::submit('Edit User', array('class'=>'button')) }}
{{ Form::close() }}
Thanks.

When you do Form::open(), it defaults to using the post request method. But when you create a Route::resource(), the edit method takes a get request.
To make it work through the form, you'll need to open it with an additional parameter, like this:
{{ Form::open(array('route' => array('users.edit',$user->id),
'method' => 'get')) }}

You need to point to the update route, not edit.
{{ Form::open(array('route' => array('users.update', $user->id))) }}
The edit route is for displaying the view, while the update is for the put/patch request.
For more information about using the RESTful routes, I'd recommend checking out http://laravel.com/docs/controllers#resource-controllers

Related

understanding how laravel interprets blade form url?

I have the below form in a larvel view:
<div id="admin">
<h1>Products Admin Panel</h1><hr>
<p>Here you can view, delete, and create new products.</p>
<h2>Products</h2><hr>
<!--admin/fileupload/create-->
{{ Form::open(array('url'=>'admin/products/create' , 'files'=>true)) }}
<p>
{{ Form::file('image') }}
</p>
{{ Form::submit('Create Product' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
</div> <!-- end admin -->
I am new to laravel and basically just want to understand , in the URL when i specify 'url'=>'admin/products/create' , what is laravel going to look for ?
a modal called products ? or a controller called products ? and a method getCreate or postCreate inside it ? what is admin then , i want to understand how laravel interprets this blade form url , can anybody explain ?
I want somebody to explain to me how does laravel interpret blade form url ?
In laravel,you will specify which url to be processed by which controller and by which method inside that controller.This specify must be do in routes.php file that is located in projectname/app/Http/routes.php .
When you specify the 'url'=>'admin/products/create' you must define the route in routes.php .
Route can be define in different ways like:
Route::get('admin/products/create','ProductController#crete');
Here you can use get or post according to your request.
Another way you can do is
Route::get('admin/products/create',array(
'as'=> 'create-product',
'uses'=>'ProductController#create'
));
Now , you can do like this route('create-product'); instead of 'url'=>'admin/products/create' .
Another way by using Route group
Route::group(['prefix'=>'admin'],function(){
Route::group(['prefix'=>'products'],function(){
Route::get('/create',array(
'as'=>'create-product',
'uses'=> 'ProductController#create'
));
// Here you can define other route that have the url like /admin/products/*
});
});
Now you can do like route('create-product') or 'url'=>'admin/products/create' Advantage
For more info check the documentation Here

Using Form button/Eloquent to set an entire column to null

I am having a real issue getting a button to scan and clear out an entire column using an eloquent model. I have two columns in my SQLite DB, "States" and "Totals"... I want the States to stay in their own order, but I want the totals to be cleared out upon the user selecting a button. The character type for 'totals' is BigInt... After the user selects the button, I want them redirected to the home page (with the values cleared so they can start over).
Here are my routes:
Route::resource('states', 'StateController');
Route::get('/', 'StateController#index');
Route::post('create', 'StateController#store');
Route::post('states.update', 'StateController#update');
Here is my controller:
public function update()
{
Distributors::update(['total' => null]);
return View::make('states');
}
Here is my form:
{{ Form::open(['route' => 'states.update']) }}
{{ Form::submit('Destroy and Start Anew') }}
{{ Form::close() }}
The error I get is:
MethodNotAllowedHttpException
Is there a simple issue with my routes? I can't figure it out.
You did not specify a method attribute on your form, so it will automatically execute a GET request. Your state.update route is only setup to accept POST requests.
Change your form to this:
{{ Form::open(['route' => 'states.update', 'method' => 'post']) }}
Please delete
Route::resource('states', 'StateController');
in your route and try again.

Hide required parameters of routes in Laravel 5.0

How could i hide the parameters of a get route in laravel 5?
I mean, a route can have required parameters, and also optional parameters, i would like to know how to hide that parameters.
Here's Laravel docs for Route parameters
You can capture segments of the request URI within your route:
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
If my domain is: example.com, when i access to example.com/user/201348 i would like that in the browser the URL be: example.com/user for example.
What you need is not a get route but a post route.
Route::get('user/', function(Request $request)
{
return 'User '.$request->get('id');
});
But keep in mind:
You need to create a form to generate a post request.
{{ Form::open(array('url' => 'user')) }}
{{ Form::hidden('id', $userId); }}
{{ Form::submit('Show user with id '.$userId); }}
{{ Form::close() }}

Populate form with Auth::user()

I'm trying to populate a form with my logged in user so I can edit some profile information.
I currently have this code (only trying to display the email first, will populate the form later once this works):
I made an editprofile.blade.php template with:
#extends('masterpage')
#section('content')
<h1> Edit Profile </h1>
{{ Form::model($user) }}
<div class="form-group">
{{ Form::label('email', 'E-mail') }}
{{ Form::Text('email', null, ['class' => 'form-control']) }}
</div>
{{ Form::close() }}
#stop
In myProfileController.php I have:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Html\FormFacade;
use Illuminate\Html\HtmlFacade;
class ProfileController extends Controller {
public function show() {
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
}
In my route I added:
Route::get('/profile/edit', 'ProfileController#show');
But when I try to load the page I get this:
I have two questions:
1: Why isn't there a form showing up with the data? As you can see the email is being loaded into the form but it displays it as text.
2. Once the data is loaded into a succesful form and the user has edited some of the data, how would I make update function to save the edited data?
I think that you are just outputting the raw data because probably you are using double brackets {{ }}.
As i remember i had a same thing when i migrated a site from laravel 4 to laravel 5. They changed the way blade output and echo the data. try to change double brackets {{ }} to this {{!! !!}} or this {{{ }}} i don't remember exactly which one it is.
Edit: about saving you can use controllers so when user post the data it goes to something like the same url just with post method, so it calls the update function inside controller.
For some functions where user should have full control over his profile like edit, update, delete, create i would use the examples from this http://laravel.com/docs/5.0/controllers#restful-resource-controllers take a look at restful controller

Laravel 4 - Multiple forms on same page?

I'm having some strange behavior with my forms in Laravel 4. I have a "settings" page with two forms, each (are supposed to) POST to a controller method, update the database and return back to the settings page. However, there seems to be an issue, either with the way my forms are working or my routes.
Here's how it is, simplified:
Settings page: (site.com/settings)
<div id="form-one" class="form-area">
{{ Form::open(array('action' => 'SettingController#editOption')) }}
{{ Form::text('optionvalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
<div id="form-two" class="form-area">
{{ Form::open(array('action' => 'SettingController#editPage')) }}
{{ Form::text('pagevalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
So basically, two seperate forms on the same page that post to two seperate methods in the same Controller - when the method is successful, it redirects them back to "settings". I won't post the methods, since tested them and they work, I believe the problem is in the routes file:
routes.php
// Checks if a session is active
Route::group(array('before' => 'require_login'), function()
{
Route::group(array('prefix' => 'settings'), function()
{
Route::get('/', 'SettingController#index');
Route::post('/', 'SettingController#editOption');
Route::post('/', 'SettingController#editPage');
});
});
Now I'm pretty sure it doesn't like the two POST routes being like that, however I cannot think of another way to do it, since the forms are on the same page. I get the error:
Unknown action [SettingController#editOption].
Since the option form comes first I guess. If I take the open form blade code out (for both), it loads the page - but obviously the form doesn't do anything.
Any help would be nice! Thanks in advance.
You can't add two same routes for different actions, because of they will be passed to first matched route and in your case to SettingController#editOption. Change your routes to :
Route::post('/option', 'SettingController#editOption');
Route::post('/page', 'SettingController#editPage');
Than in both actions you can redirect to '/': return Redirect::back(), and if error was occured:
if ($validation->fails())
{
return Redirect::to('/settings')->with_errors($validation);
}
My alternative solution for this is to create an hidden html input in each form and make the controller identify what for is submitted based in this field. So, yu can use just one route for both.

Categories