Laravel 4 - Creating a Simple CRUD Application in Hours

Exclusive offer: get 50% off this eBook here
Learning Laravel 4 Application Development

Learning Laravel 4 Application Development — Save 50%

Develop real-world web applications in Laravel 4 using its refined and expressive syntax with this book and ebook

£18.99    £9.50
by Hardik Dangar | December 2013 | Open Source Web Development

In this article by Hardik Dangar, author of the book Learning Laravel 4 Application Development, let's start working our way into our first simple CRUD application. We will see how Controllers and routes work and how we can create views. Then we will build our first CRUD application in hours and not in days via some artisan-powered magic. Here are the topics we are going to cover in this article.

  • Getting familiar with Laravel 4
  • Creating a simple CRUD application with Laravel 4
    • Listing the users
    • Creating new users
    • Editing user information
    • Deleting user information
    • Adding pagination to our list users

(for more resources related to this topic, see here.)

Getting familiar with Laravel 4

Let's Begin the Journey, and installed Laravel 4. Now if everything is installed correctly you will be greeted by this beautiful screen, as shown in the following screenshot, when you hit your browser with http://localhost/laravel/public or http://localhost/<installeddirectory>/public:

Now that you can see we have installed Laravel correctly, you would be thinking how can I use Laravel? How do I create apps with Laravel? Or you might be wondering why and how this screen is shown to us? What's behind the scenes? How Laravel 4 sets this screen for us? So let's review that.

When you visit the http://localhost/laravel/public, Laravel 4 detects that you are requesting for the default route which is "/". You would be wondering what route is this if you are not familiar with the MVC world. Let me explain that.

In traditional web applications we use a URL with page name, say for example:

http://www.shop.com/products.php

The preceding URL will be bound to the page products.php in the web server hosting shop.com. We can assume that it displays all the products from the database. Now say for example, we want to display a category of books from all the products. You will say, "Hey, it's easy!" Just add the category ID into the URL as follows:

http://www.shop.com/products.php?cat=1

Then put the filter in the page products.php that will check whether the category ID is passed. This sounds perfect, but what about pagination and other categories? Soon clients will ask you to change one of your category page layouts to change and you will hack your code more. And your application URLs will look like the following:

If you look at your code after six months, you would be looking at one huge products.php page with all of your business and view code mixed in one large file. You wouldn't remember those easy hacks you did in order to manage client requests. On top of that, a client or client's SEO executive might ask you why are all the URLs so badly formatted? Why are they are not human friendly? In a way they are right. Your URLs are not as pretty as the following:

The preceding URLs are human friendly. Users can easily change categories themselves. In addition to that, your client's SEO executives will love you for those URLs just as a search engine likes those URLs.

You might be puzzled now; how do you do that? Here my friend MVC (Model View Controller) comes into the picture. MVC frameworks are meant specifically for doing this. It's one of the core goals of using the MVC framework in web development.

So let's go back to our topic "routing"; routing means decoupling your URL request and assigning it to some specific action via your controller/route. In the Laravel MVC world, you register all your routes in a route file and assign an action to them. All your routes are generally found at /app/routes.php.

If you open your newly downloaded Laravel installation's routes.php file, you will notice the following code:

Route::get('/', function() { return View::make('hello'); });

The preceding code registers a route with / means default URL with view /app/views/hello.php. Here view is just an .html file. Generally view files are used for managing your presentation logic. So check /app/views/hello.php, or better let's create an about page for our application ourselves.

Let's register a route about by adding the following code to app/routes.php:

Route::get('about', function() { return View::make('about'); });

We would need to create a view at app/views/about.php. So create the file and insert the following code in to it:

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>About my little app</title> </head> <body> <h1>Hello Laravel 4!</h1> <p> Welcome to the Awesomeness! </p> </body> </html>

Now head over to your browser and run http://localhost/laravel/public/about. You will be greeted with the following output:

Hello Laravel 4!

Welcome to the Awesomeness!

Isn't it easy? You can define your route and separate the view for each type of request. Now you might be thinking what about Controllers as the term MVC has C for Controllers? And isn't it difficult to create routes and views for each action? What advantage will we have if we use the preceding pattern? Well we found that mapping URLs to a particular action in comparison to the traditional one-file-based method. Well first you are organizing your code way better as you will have actions responding to specific URLs mapped in the route file.

Any developer can recognize routes and see what's going on with your code. Developers do not have to check many files to see which files are using which code. Your presentation logic is separated, so if a designer wants to change something, he will know he needs to look at the view folder of your application.

Now about Controllers; they allow us to group related actions into a single class. So in a typical MVC project, there will be one user Controller that will be responsible for all user-related actions, such as registering, logging in, editing a profile, and changing the password. Generally routes are used for small applications or creating static pages quickly. Controllers provide more in-depth options to create a group of methods that belong to a specific class related to the application.

Here is how we can create Controllers in Laravel 4. Open your app/routes.php file and add following code:

Route::get('contact', 'Pages@contact');

The preceding code will register the http://yourapp.com/contact URL in the Pages Controller's contact method. So let's write a page's Controller. Create a file PagesController.php at /app/controllers/ in your Laravel 4 installation directory. The following are the contents of the PagesController.php file:

<?php class PagesController extends BaseController { public function contact() { return View::make('hello'); } }

Here BaseController is a class provided by Laravel so we can place our Controller shared logic in a common class. And it extends the framework's Controller class and provides the Controller functionality. You can check Basecontroller.php in the Controller's directory to add shared logic.

Controllers versus routes

So you are wondering now, "What's the difference between Controllers and routes?" Which one to use? Controllers or routes? Here are the differences between Controllers and routes:

  • A disadvantage of routes is that you can't share code between routes, as routes work via Closure functions. And the scope of a function is bound within function.
  • Controllers give a structure to your code. You can define your system in well-grouped classes, which are divided in such a way that it makes sense, for example, users, dashboard, products, and so on.
  • Compared to routes, Controllers have only one disadvantage and it's that you have to create a file for each Controller; however, if you think in terms of organizing the code in a large application, it makes more sense to use Controllers.

Creating a simple CRUD application with Laravel 4

Now as we have a basic understanding of how we can create pages, let's create a simple CRUD application with Laravel 4. The application we want to create will manage the users of our application. We will create the following list of features for our application:

  • List users (read users from the database)
  • Create new users
  • Edit user information
  • Delete user information
  • Adding pagination to the list of users

Now to start off with things, we would need to set up a database. So if you have phpMyAdmin installed with your local web server setup, head over to http://localhost/phpmyadmin; if you don't have phpMyAdmin installed, use the MySQL admin tool workbench to connect with your database and create a new database.

Now we need to configure Laravel 4 to connect with our database. So head over to your Laravel 4 application folder, open /app/config/database.php, change the MySQL array, and match your current database settings. Here is the MySQL database array from database.php file:

'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => '<yourdbname>', 'username' => 'root', 'password' => '<yourmysqlpassord>', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),

Now we are ready to work with the database in our application. Let's first create the database table Users via the following SQL queries from phpMyAdmin or any MySQL database admin tool;

CREATE TABLE IF NOT EXISTS 'users' ( 'id' int(10) unsigned NOT NULL AUTO_INCREMENT, 'username' varchar(255) COLLATE utf8_unicode_ci NOT NULL, 'password' varchar(255) COLLATE utf8_unicode_ci NOT NULL, 'email' varchar(255) COLLATE utf8_unicode_ci NOT NULL, 'phone' varchar(255) COLLATE utf8_unicode_ci NOT NULL, 'name' varchar(255) COLLATE utf8_unicode_ci NOT NULL, 'created_at' timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 'updated_at' timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY ('id') ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=3 ;

Now let's seed some data into the Users table so when we fetch the users we won't get empty results. Run the following queries into your database admin tool:

INSERT INTO 'users' ('id', 'username', 'password', 'email', 'phone',
'name', 'created_at', 'updated_at') VALUES (1, 'john', 'johndoe', 'johndoe@gmail.com', '123456', 'John',
'2013-06-07 08:13:28', '2013-06-07 08:13:28'), (2, 'amy', 'amy.deg', 'amy@outlook.com', '1234567', 'amy',
'2013-06-07 08:14:49', '2013-06-07 08:14:49');

Listing the users – read users from database

Let's read users from the database. We would need to follow the steps described to read users from database:

  • A route that will lead to our page
  • A controller that will handle our method
  • The Eloquent Model that will connect to the database
  • A view that will display our records in the template

So let's create our route at /app/routes.php. Add the following line to the routes.php file:

Route::resource('users', 'UserController');

If you have noticed previously, we had Route::get for displaying our page Controller. But now we are using resource. So what's the difference?

In general we face two types of requests during web projects: GET and POST. We generally use these HTTP request types to manipulate our pages, that is, you will check whether the page has any POST variables set; if not, you will display the user form to enter data. As a user submits the form, it will send a POST request as we generally define the <form method="post"> tag in our pages. Now based on page's request type, we set the code to perform actions such as inserting user data into our database or filtering records.

What Laravel provides us is that we can simply tap into either a GET or POST request via routes and send it to the appropriate method. Here is an example for that:

Route::get('/register', 'UserController@showUserRegistration'); Route::post('/register', 'UserController@saveUser');

See the difference here is we are registering the same URL, /register, but we are defining its GET method so Laravel can call UserController class' showUserRegistration method. If it's the POST method, Laravel should call the saveUser method of the UserController class.

You might be wondering what's the benefit of it? Well six months later if you want to know how something's happening in your app, you can just check out the routes.php file and guess which Controller and which method of Controller handles the part you are interested in, developing it further or solving some bug. Even some other developer who is not used to your project will be able to understand how things work and can easily help move your project. This is because he would be able to somewhat understand the structure of your application by checking routes.php.

Now imagine the routes you will need for editing, deleting, or displaying a user. Resource Controller will save you from this trouble. A single line of route will map multiple restful actions with our resource Controller. It will automatically map the following actions with HTTP verbs:

HTTP VERB

ACTION

GET

READ

POST

CREATE

PUT

UPDATE

DELETE

DELETE

On top of that you can actually generate your Controller via a simple command-line artisan using the following command:

$ php artisan Usercontroller:make users

This will generate UsersController.php with all the RESTful empty methods, so you will have an empty structure to play with. Here is what we will have after the preceding command:

class UserController extends BaseController { /** * Display a listing of the resource. * * @return Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return Response */ public function create() { // } /** * Store a newly created resource in storage. * * @return Response */ public function store() { // } /** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // } }

Now let's try to understand what our single line route declaration created relationship with our generated Controller.

HTTP VERB

Path

Controller Action/method

GET

/Users

Index

GET

/Users/create

Create

POST

/Users

Store

GET

/Users/{id}

Show (individual record)

GET

/Users/{id}/edit

Edit

PUT

/Users/{id}

Update

DELETE

/Users/{id}

Destroy

As you can see, resource Controller really makes your work easy. You don't have to create lots of routes. Also Laravel 4's artisan-command-line generator can generate resourceful Controllers, so you will write very less boilerplate code. And you can also use the following command to view the list of all the routes in your project from the root of your project, launching command line:

$ php artisan routes

Now let's get back to our basic task, that is, reading users. Well now we know that we have UserController.php at /app/controller with the index method, which will be executed when somebody launches http://localhost/laravel/public/users. So let's edit the Controller file to fetch data from the database.

Well as you might remember, we will need a Model to do that. But how do we define one and what's the use of Models? You might be wondering, can't we just run the queries? Well Laravel does support queries through the DB class, but Laravel also has Eloquent that gives us our table as a database object, and what's great about object is that we can play around with its methods. So let's create a Model.

If you check your path /app/models/User.php, you will already have a user Model defined. It's there because Laravel provides us with some basic user authentication. Generally you can create your Model using the following code:

class User extends Eloquent {}

Now in your controller you can fetch the user object using the following code:

$users = User::all(); $users->toarray();

Yeah! It's that simple. No database connection! No queries! Isn't it magic? It's the simplicity of Eloquent objects that many people like in Laravel.

But you have the following questions, right?

  • How does Model know which table to fetch?
  • How does Controller know what is a user?
  • How does the fetching of user records work? We don't have all the methods in the User class, so how did it work?

Well models in Laravel use a lowercase, plural name of the class as the table name unless another name is explicitly specified. So in our case, User was converted to a lowercase user and used as a table to bind with the User class.

Models are automatically loaded by Laravel, so you don't have to include the reference of the Model file. Each Model inherits an Eloquent instance that resolves methods defined in the model.php file at vendor/Laravel/framework/src/Illumininate/Database/Eloquent/ like all, insert, update, delete and our user class inherit those methods and as a result of this, we can fetch records via User::all().

So now let's try to fetch users from our database via the Eloquent object. I am updating the index method in our app/controllers/UsersController.php as it's the method responsible as per the REST convention we are using via resource Controller.

public function index() { $users = User::all(); return View::make('users.index', compact('users')); }

Now let's look at the View part. Before that, we need to know about Blade. Blade is a templating engine provided by Laravel. Blade has a very simple syntax, and you can determine most of the Blade expressions within your view files as they begin with @. To print anything with Blade, you can use the {{ $var }} syntax. Its PHP-equivalent syntax would be:

<?php echo $var; ?>

Now back to our view; first of all, we need to create a view file at /app/views/users/index.blade.php, as our statement would return the view file from users.index. We are passing a compact users array to this view. So here is our index.blade.php file:

@section('main') <h1>All Users</h1> <p>{{ link_to_route('users.create', 'Add new user') }}</p> @if ($users->count()) <table class="table table-striped table-bordered"> <thead> <tr> <th>Username</th> <th>Password</th> <th>Email</th> <th>Phone</th> <th>Name</th> </tr> </thead> <tbody> @foreach ($users as $user) <tr> <td>{{ $user->username }}</td> <td>{{ $user->password }}</td> <td>{{ $user->email }}</td> <td>{{ $user->phone }}</td> <td>{{ $user->name }}</td> <td>{{ link_to_route('users.edit', 'Edit',
array($user->id), array('class' => 'btn btn-info')) }}</td> <td> {{ Form::open(array('method' => 'DELETE', 'route' => array('users.destroy', $user->id))) }} {{ Form::submit('Delete', array('class'
=> 'btn btn-danger')) }} {{ Form::close() }} </td> </tr> @endforeach </tbody> </table> @else There are no users @endif @stop

Let's see the code line by line. In the first line we are extending the user layouts via the Blade template syntax @extends. What actually happens here is that Laravel will load the layout file at /app/views/layouts/user.blade.php first.

Here is our user.blade.php file's code:

<!doctype html> <html> <head> <meta charset="utf-8"> <link href="//netdna.bootstrapcdn.com/twitter-bootstrap
/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"> <style> table form { margin-bottom: 0; } form ul { margin-left: 0; list-style: none; } .error { color: red; font-style: italic; } body { padding-top: 20px; } </style> </head> <body> <div class="container"> @if (Session::has('message')) <div class="flash alert"> <p>{{ Session::get('message') }}</p> </div> @endif @yield('main') </div> </body> </html>

Now in this file we are loading the Twitter bootstrap framework for styling our page, and via yield('main') we can load the main section from the view that is loaded. So here when we load http://localhost/laravel/public/users, Laravel will first load the users.blade.php layout view and then the main section will be loaded from index.blade.php.

Now when we get back to our index.blade.php, we have the main section defined as @section('main'), which will be used by Laravel to load it into our layout file. This section will be merged into the layout file where we have put the @yield ('main') section.

We are using Laravel's link_to_route method to link to our route, that is, /users/create. This helper will generate an HTML link with the correct URL. In the next step, we are looping through all the user records and displaying it simply in a tabular format. Now if you have followed everything, you will be greeted by the following screen:

Learning Laravel 4 Application Development Develop real-world web applications in Laravel 4 using its refined and expressive syntax with this book and ebook
Published: December 2013
eBook Price: £18.99
Book Price: £30.99
See more
Select your format and quantity:

Creating new users

Now as we have listed our users, let's write the code for creating new users. To create a new user, we will need to create the Controller method and bind the view for displaying the new user form. We would need to create the Controller method for saving the user too. Now as we have bound the resourceful Controller, we don't need to create separate routes for each of our request. Laravel will handle that part if we use REST methods.

So first let's edit the controller at /app/controllers/UsersController.php to add a method for displaying the view:

public function create() { return View::make('users.create'); }

This will call a view at /app/views/users/create.blade.php. So let's define our create.blade.php view as follows:

@extends('layouts.users') @section('main') <h1>Create User</h1> {{ Form::open(array('route' => 'users.store')) }} <ul> <li> {{ Form::label('name', 'Name:') }} {{ Form::text('name') }} </li> <li> {{ Form::label('username', 'Username:') }} {{ Form::text('username') }} </li> <li> {{ Form::label('password', 'Password:') }} {{ Form::password('password') }} </li> <li> {{ Form::label('password', 'Confirm Password:') }} {{ Form::password('password_confirmation') }} </li> <li> {{ Form::label('email', 'Email:') }} {{ Form::text('email') }} </li> <li> {{ Form::label('phone', 'Phone:') }} {{ Form::text('phone') }} </li> <li> {{ Form::submit('Submit', array('class' => 'btn')) }} </li> </ul> {{ Form::close() }} @if ($errors->any()) <ul> {{ implode('', $errors->all('<li class="error">:message</li>')) }} </ul> @endif @stop

Let's try to understand our preceding view. Here we are extending the users layout we created in our List Users section. Now in the main section, we are using Laravel's Form helper to generate our form. This helper generates HTML code via its methods such as label, text, and submit.

Refer to the following code:

{{ Form::open(array('route' => 'users.store')) }}

The preceding code will generate the following HTML code:

<form method="POST" action="http://localhost/users" accept-charset="UTF-8">

As you can see it's really convenient for us to not worry about linking things correctly. Now let's create our store method to store our form data into our users table:

public function store() { $input = Input::all(); $validation = Validator::make($input, User::$rules); if ($validation->passes()) { User::create($input); return Redirect::route('users.index'); } return Redirect::route('users.create') ->withInput() ->withErrors($validation) ->with('message', 'There were validation errors.'); }

Here we are first validating all the input that came from the user. The Input::all () function fetches all the $_GET and $_POST variables and puts it into a single array. The reason why we are creating the single input array is so we can check that array against validation rules' array. Laravel provides a very simple Validation class that can be used to check validations. We could use it to check whether validations provided in the rules array are followed by the input array by using the following line of code:

$validation = Validator::make ($input, User::$rules);

Rules can be defined in an array with validation attributes separated by the column "|". Here we are using User::$rules where User is our Model and it will have following code:

class User extends Eloquent { protected $guarded = array('id'); protected $fillable = array('name', 'email'); public static $rules = array( 'name' => 'required|min:5', 'email' => 'required|email' ); }

As you can observe we have defined two rules mainly for name and e-mail input fields. If you are wondering about $guarded and $fillable variables, these variables are used to prevent mass assignment. When you pass an array into your Model's create and update methods, Laravel tries to match the right columns and sets values in the database. Now for instance, if a malicious user sends a hidden input named id and changes his ID via the update method of your form, it could be a huge security hole; to prevent this, we should define the $guarded and $fillable arrays. The $guarded array will guard the columns defined in the guarded array, that is, it will prevent anyone from changing values in that column. The $fillable array will only allow elements defined in $fillable to be updated.

Now we can use the $validation instance we created to check for validations.

$result = $validation->passes(); echo $result; // True or false

If you see our code now, we are checking for validation via the passes() method in our Store() method of UserController. Now if validation gets passed, we can use our user Model to store data into our database. All you need to do is call the Create method of the Model class with the $input array. So refer to the following code:

User::create($input);

The preceding code will store our $input array into the database; yes, it's equivalent to your SQL query.

Insert into user(name,password,email,city) values (x,x,..,x);

Here we have to fill either the $fillable or $guarded array in the model, otherwise, Laravel will throw a mass assignment exception. Laravel's Eloquent object automatically matches our input array with the database and creates a query based on our input array. Don't you think this is a simple way to store input into the database? If user data is inserted, we are using Laravel's redirect method to redirect it to our list of users' pages. If validation fails, we are sending all of the input with errors from the validation object into our create users form.

Editing user information

Now as we learned how easy it is to add and list users. Let's jump into editing a user. In our list users view we have the edit link with the following code:

{{ link_to_route('users.edit', 'Edit', array($user->id),
array('class' => 'btn btn-info')) }}

Here, the link_to_route function will generate a link /users/<id>/edit, which will call the resourceful Controller user, and Controller will bind it with the edit method.

So here is the code for editing a user. First of all we are handling the edit request by adding the following code to our UsersController:

public function edit($id) { $user = User::find($id); if (is_null($user)) { return Redirect::route('users.index'); } return View::make('users.edit', compact('user')); }

So when the edit request is fired, it will hit the edit method described in the preceding code snippet. We would need to find whether the user exists in the database. So we use our user model to query the ID using the following line of code:

$user = User::find($id);

Eloquent object's find method will query the database just like a normal SQL.

Select * from users where id = $id

Then we will check whether the object we received is empty or not. If it is empty, we would just redirect the user to our list user's interface. If it is not empty, we would direct the user to the user's edit view with our Eloquent object as a compact array.

So let's create our edit user view at app/views/users/edit.blade.php, as follows:

@extends('users.scaffold') @section('main') <h1>Edit User</h1> {{ Form::model($user, array('method' => 'PATCH', 'route' =>
array('users.update', $user->id))) }} <ul> <li> {{ Form::label('username', 'Username:') }} {{ Form::text('username') }} </li> <li> {{ Form::label('password', 'Password:') }} {{ Form::text('password') }} </li> <li> {{ Form::label('email', 'Email:') }} {{ Form::text('email') }} </li> <li> {{ Form::label('phone', 'Phone:') }} {{ Form::text('phone') }} </li> <li> {{ Form::label('name', 'Name:') }} {{ Form::text('name') }} </li> <li> {{ Form::submit('Update', array('class' => 'btn btn-info')) }} {{ link_to_route('users.show', 'Cancel', $user->
id, array('class' => 'btn')) }} </li> </ul> {{ Form::close() }} @if ($errors->any()) <ul> {{ implode('', $errors->all('<li class="error">:message</li>')) }} </ul> @endif @stop

Here we are extending our users' layout as always and defining the main section. Now in the main section, we are using the Form helper to generate a proper REST request for our controller.

{{ Form::model($user, array('method' => 'PATCH', 'route' =>
array('users.update', $user->id))) }}

Now, you may have not dealt with the method PATCH as we only know of two protocols, GET and POST, as most browsers generally support only these two methods. The REST method for editing is PATCH, and what Laravel does is that it creates a hidden token so it knows which method to call. So the preceding code generates the following code:

<form method="POST" action="http://ch3.sr/users/1" accept-charset="UTF-8"> <input name="_method" type="hidden" value="PATCH">

It actually fires a POST method for browsers that are not capable for handling the PATCH method. Now, when a user submits this form, it will send a request to the update method of UsersController via the resourceful Controller we set in routes.php.

Here is the update method of UsersController:

public function update($id) { $input = Input::all(); $validation = Validator::make($input, User::$rules); if ($validation->passes()) { $user = User::find($id); $user->update($input); return Redirect::route('users.show', $id); } return Redirect::route('users.edit', $id) ->withInput() ->withErrors($validation) ->with('message', 'There were validation errors.'); }

Here Laravel will pass the ID of the user we are editing in the update method. We can use this ID to find the user via our user model's Eloquent object's find method. Then we will update the Eloquent object with an input array just like we did in the insert operation.

Deleting user information

To delete a user we can use the destroy method. If you go to our user lists view, you can find the following delete link's generation code:

{{ Form::open(array('method' => 'DELETE', 'route' =>
array('users.destroy', $user->id))) }} {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }} {{ Form::close() }}

The preceding code is handled by Laravel similar to the way in which it handles the PATCH method. Laravel will generate a post request with the hidden method token set as PATCH, which it can recognize when it hits the Laravel request object.

At UsersController this request will hit the destroy() method as follows:

public function destroy($id) { User::find($id)->delete(); return Redirect::route('users.index'); }

Laravel will directly send id to the destroy method, so all we have to do is use our user model and delete the record with its delete method. If you noticed Eloquent allows us to chain methods. Isn't it sweet? So there would be no queries but just one line to delete a user.

That's one of the reasons to use Eloquent objects in your projects. It allows you to quickly interact with the database and you can use objects to match your business logic.

Adding pagination to our list users

One of the painful tasks most developers face often is that of pagination. With Laravel it's no more the case, as Laravel provides a simple approach to set pagination to your pages.

Let's try to implement pagination to our list user's method. To set pagination we can use the paginate method with Laravel's Eloquent object. Here is how we can do that:

public function index() { $users = User::paginate(5); return View::make('users.index', compact('users')); }

The preceding code is the index method of the UsersController class, which we were using previously for getting all the users with User::all(). We just used the paginate method to find five records from the database. Now in our list users view, we can use the following code to display pagination links:

{{ echo $users->links(); }} Here the links() method will generate pagination links for you.
And best part, Laravel will manage the code for pagination.
So all you have to do is use paginate method with
your eloquent object and links method to display generated links.

Summary

So we have set up our simple CRUD application and now we know how easy it is with Laravel to set up CRUD operations. We have seen Eloquent Laravel's database ORM that makes working with a database simple and easy. We have learned how to list users, create new users, edit users, delete users, and how to add pagination to our application.

resources for article:


further resources on this subject:


Learning Laravel 4 Application Development Develop real-world web applications in Laravel 4 using its refined and expressive syntax with this book and ebook
Published: December 2013
eBook Price: £18.99
Book Price: £30.99
See more
Select your format and quantity:

About the Author :


Hardik Dangar

Hardik Dangar is co-founder of Sphere Rays Technolabs Pvt. Ltd. His primary focus has been on web-based ERP systems for the last six years. Lately, he has been working with a few of the key startups to shape their core ideas into architectural development and implementation of projects from inception. He can be found on Google+ developer communities and IRC channels of various frameworks helping people and building communities.

Books From Packt


 Laravel Application Development Blueprints
Laravel Application Development Blueprints

 Laravel Starter [Instant]
Laravel Starter [Instant]

 Laravel Application Development Cookbook
Laravel Application Development Cookbook

 Learning FuelPHP for Effective PHP Development
Learning FuelPHP for Effective PHP Development

 RESTful PHP Web Services
RESTful PHP Web Services

Persistence in PHP with Doctrine ORM
Persistence in PHP with Doctrine ORM

 Building Websites with PHP-Nuke
Building Websites with PHP-Nuke

 PHP Team Development
PHP Team Development


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software