21.11.2014 Views

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Laravel</strong> <strong>Starter</strong><br />

Step 17 – Creating the update form with the form helper<br />

Now, we just need to create the update form and we'll have a full administrative system!<br />

Go ahead and create the view at application/views/users/update.blade.php<br />

and fill it with this lovely form:<br />

Update a <strong>User</strong><br />

{{ Form::open() }}<br />

Real Name: {{ Form::text('real_name', $user->real_name) }}<br />

Email: {{ Form::text('email', $user->email) }}<br />

Change Password: {{ Form::password('password') }}<br />

{{ Form::submit('Update <strong>User</strong>') }}<br />

{{ Form::close() }}<br />

This is almost exactly like the create form except that we have mixed things up a little. First of<br />

all, you'll notice that we're using <strong>Laravel</strong>'s Form class helper methods. These helper methods,<br />

like the HTML class' helper methods, are not mandatory. However, they are recommended. They<br />

offer many of the same advantages as the HTML class' helper methods. The Form class' helper<br />

methods offer a unified interface for generating the resulting HTML tags. It's much easier to<br />

programmatically update HTML tag attributes by passing an array as an argument than to loop<br />

through and generate the HTML yourself.<br />

Real Name: {{ Form::text('real_name', $user->real_name) }}<br />

Text fields can be prepopulated by passing in a second argument. In this example, we're passing<br />

the real_name attribute from the user object that we passed from the controller. We then<br />

prepopulate the email field in the same way.<br />

Change Password: {{ Form::password('password') }}<br />

Notice that we're not prepopulating the password field. It doesn't make sense to do so as<br />

we're not storing a readable version of the password in the database. Not only that, to prevent<br />

a developer from making a mistake the Form::password() method does not have the<br />

functionality to prepopulate this field at all.<br />

And with that we have a fully working update user form!<br />

26

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!