03.12.2015 Views

laravel4cookbook

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

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

Access Control List 53<br />

.<br />

This file should be saved as app/views/group/index.blade.php.<br />

We’ve modified the group/index view to include two links; which will redirect users either to the<br />

edit page or the delete action. Notice the class=”confirm” and data-confirm=”…” attributes we’ve<br />

added to the delete link — we’ll use these shortly. We’ll also need to add the delete route to the<br />

routes.php file…<br />

1 Route::any("/group/delete", [<br />

2 "as" => "group/delete",<br />

3 "uses" => "GroupController@deleteAction"<br />

4 ]);<br />

.<br />

This was extracted from app/routes.php.<br />

Since we’ve chosen such an easy method of deleting groups, the action is pretty straightforward:<br />

1 public function deleteAction()<br />

2 {<br />

3 $form = new GroupForm();<br />

4<br />

5 if ($form->isValidForDelete())<br />

6 {<br />

7 $group = Group::findOrFail(Input::get("id"));<br />

8 $group->delete();<br />

9 }<br />

10<br />

11 return Redirect::route("group/index");<br />

12 }<br />

.<br />

This was extracted from app/controllers/GroupController.php.<br />

We simply need to find a group with the provided id (using the findOrFail() method we saw earlier)<br />

and delete it. After that; we redirect back to the listing page. Before we take this for a spin, let’s add<br />

the following JavaScript:

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

Saved successfully!

Ooh no, something went wrong!