automatic method POST or PUT in laravel form collective - php

I am using laravel form collective for automatic form generation in project.
I used same form for add and update with route model binding
{!! Form::model($operator, ['route' => ['operator.update', $operator->id]]) !!}
and route for this is
Route::resource('operator','OperatorController');
This generates automatic routes for method like POST for store and PUT for updates
if I use form collective then by default method is POST for both store and update
if I change to PUT then it changed for both
I need automatic method detection like if I am using for store, it should be POST and PUT if it is edit form
can we do this ?

Mainly if you want to detect both of this situation you should use Form::open for create mode and Form::model for update mode (with method PUT) like this:
#if(!empty($operator))
{!! Form::model($operator, ['route' => ['operator.update', $operator->id], 'method' => 'PUT']) !!}
#else
{!! Form::open(['route' => ['operator.store', $operator->id]]) !!}
#endif
This above will automaticlly add _method hidden field to your form (with PUT value) on updating.
You should also split for to actions (store and update) as it's in Laravel Docs about Resource Controller.

Related

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.

Laravel 4.1 - passing resource ID from form <select> to controller method?

Sorry if this is a stupid question. I'm very new to laravel / MVC, and haven't had enough coffee today, so I wouldn't be surprised if the answer is sitting right in front of me. :)
background: I have a form with a select. The form is used to delete a "user" and all their associated resources from the database. The <select> is populated with unique ID's. on form submit, I would like to send a DELETE request to my Controller class, passing in the selected ID for deletion.
I can't figure out how to pass in the ID from the select, into the form. How do I make it so that when you select (for example) ID 1 in the drop down, that is passed into my resource routing on the form?
Here's some code:
{{ Form::open(array(
'url' =>'/clients',
'method'=>'delete',
'name' =>'delClient',
'role' =>'form',
'class' =>'form-horizontal')) }}
<h4>Please select the client you would like to delete:</h4>
{{
Form::selectField('delClientSelect', 'Client: ', array(
0=>'-- Select Client --')+$clientsList)
}}
{{
Form::submit('Delete Selected', array(
'class' => 'btn btn-danger confDelClient',
'data-role'=> 'delete'
))
}}
{{ Form::close() }}
Everything I read online says that you pass the id into the Form 'url' attribute, i.e:
{{
Form::open(array('url'=>'/clients/{id}'))
}}
but, as the ID is coming from the <select>, I'm not sure what the proper method is for getting the ID into my routing.
Thanks for any help!
If you are using a resource controller where you must use the DELETE verb to delete a record, then I would use jQuery to update the form URL/action to 'url'=>'/clients/{id}' as you stated.
Basically, on form submit (with jQuery that's $('form').submit()) you can append the ID from the select field to the forms url/action.
If you aren't using a resource controller, I would use the POST method, grab the ID from the select using Input::all() or Input::get() and then make your database call to delete the records from the database.

laravel have both Input::old('') and value of text input on form

I have a form that will pull data from the database as well as submit new data to overwrite the old all in the same fields. For example:
{{ Form::text('date', Input::old('date'), array('id' => 'date'))}}
Where the second parameter includes both the value from the database $i->date and also the input:old validator to ensure it wasn't left blank by accident.
Is there a way to do this? I already tried using an array as the second parameter.
Yes, you should consider form model binding Form::model instead of Form::open.
Also you can leave your input value alone:
{{ Form::text('date', null, array('id' => 'date'))}}
Controller side example:
$model = new Model;
return View::make('layout', compact('model'));
Way of opening the form:
{{ Form::model($model) }}
Not exactly sure what you are trying to do but will this work?
Form model binding
{{ Form::model($yourmodel, array('route' => array('yourmodel.update', $yourmodel->id))) }}
{{ Form::text('date', Input::old('date'), null, array('id' => 'date')) }}
from the docs
If there is an item in the Session flash data matching the input
name, that will take precedence over the model's value. So, the
priority looks like this:
Session Flash Data (Old Input) Explicitly Passed Value Model Attribute
Data

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.

Laravel 4 Form::open set action

I'm currently trying out Laravel 4 and I have created a resource controller. In the 'edit' function I'm building a form, which should post to the 'update' function.
To create the form open tag I use the Form::open() function which recently got added to Laravel 4 it seems.
But when I just do Form::open() the action of the form is the current url and I can't figure out how to change the action.
I tried Form::open('clients/' . $client->id) but this gives me the following error:
ErrorException: Catchable Fatal Error: Argument 1 passed to Illuminate\Html\FormBuilder::open() must be of the type array
So I tried Form::open('[clients/' . $client->id). This doesn't generate an error, but now the form open tag is:
<form method="POST" action="http://boekhouding.dev/clients/1/edit" accept-charset="UTF-8" clients/1="clients/1">
And I also tried it like this: Form::open(['action' => 'clients/' . $client->id]) but when I do it like this, the form open tag has no action at all.
So, does anyone know how to set the form action? Using a named route would be perfect, but even being able to set the action at all would be nice.
You can use named route, controller action or simple url to set form action.
To set it via named route use:
{{ Form::open(array('route' => array('route_name', $client->id))) }}
To set it via controller action use:
{{ Form::open(array('action' => array('ClientController#update', $client->id))) }}
So the keyword action does not reffer to 'action' parameter of form tag, but to controller action
And you can also use plain URL like this:
{{ Form::open(array('url' => 'someurl')) }}
#jeffrey_way tweeted about improving the new FormBuilder in Laravel 4. The following paste bucket link should help. It seems to be more about RESTful controllers, but relavant.
Form action sensible defaults - paste bucket
I thought I read something about him coming out with a Forms tutorial tomorrow. If so, it might be found here net.tutsplus.com/?s=laravel

Categories