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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

<strong>Laravel</strong>'s HTML class can be used to create a variety of HTML tags. You might be asking yourself<br />

why you wouldn't simply write the HTML for the link yourself. One very good reason to use<br />

<strong>Laravel</strong>'s HTML helper class is that it provides a unified interface for creating tags that may<br />

need to change dynamically. Let's look at an example to clarify this point.<br />

Let's say that we want that link to look like a button and our designer created a sweet CSS class<br />

named btn. We need to update the call to HTML::link() to include the new class attribute:<br />

{{ HTML::link('users/create', 'Create a <strong>User</strong>', array('class' =><br />

'btn')) }}<br />

Actually, we could include any number of attributes to that class and they'd all be handled<br />

appropriately. Any attribute assigned to the HTML elements can be updated dynamically<br />

by passing a variable to that method instead of declaring it inline.<br />

<br />

{{ HTML::link('users/create', 'Create a <strong>User</strong>', $create_link_<br />

attributes) }}<br />

Step 15 – Deleting user records with Eloquent<br />

Now that we can add users, we may want to do a bit of cleanup. Let's add a delete action to<br />

our users controller.<br />

public function get_delete($user_id)<br />

{<br />

$user = <strong>User</strong>::find($user_id);<br />

if(is_null($user))<br />

{<br />

return Redirect::to('users');<br />

}<br />

$user->delete();<br />

}<br />

return Redirect::to('users');<br />

Now, let's step through this.<br />

public function get_delete($user_id)<br />

This is the first time that we've declared a parameter in a controller action. In order to delete a user,<br />

we need to know which user to delete. Since we have used Route::controller('users') to<br />

have <strong>Laravel</strong> automatically handle the routing for our controller, it'll know that when we go to the<br />

URL http://myfirst.dev/users/delete/1 it should route to the delete action and pass<br />

additional URI segments as arguments to the method.<br />

23

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

Saved successfully!

Ooh no, something went wrong!