Php framework Laravel makes life easy for programmers of webapplications. In our previous blog ‘Laravel: Framework for the development of webapplications (part 1)‘ we already showed you how to keep your database up-to-date with version control and the use of configuration files for different development environments.
Apart from these very nice features, Laravel makes it possible to easily perform create, read, update and delete (CRUD) operations. Those type of actions are very common in any type of webapplication and performing them quickly speeds up your workflow. Laravel has included the Eloquent ORM: after creation of a database table (using a migration) and a corresponding model, CRUD operation can be performed on this model. For example, your application has users and those users need to be able to view and adjust their contact details.
For a given user id, all details of this user can be fetched with the following command:
$user = User::find($id);
The details of this user may then be changed by doing the following:
$user->phonenumber = ‘new-phonenumber’; $user->save();
To create a new user for your application, a similar approach can be followed. The following lines of code will create a new user and save its first and last name to the database:
$user = new User; $user->firstname = ‘first name’; $user->lastname = ‘last name’; $user->save();
When creating a new database table via a Laravel migration, timestamps may be added to this table by using the ->timestamps() function. Laravel then adds the columns ‘created_at’ and ‘updated_at’ to this database table. For any new record in the table, or any update of an existing record, these timestamps are automatically updated. This way, it’s easy to see at which date and time a user had registered for your application, or when a user last changed its contact details.
Sometimes a user needs to be removed from your application, or be blocked temporarily. Removal of users can be done in two ways. If you want to permanently remove a user, the following line of code can be used:
Another possibility is to is to use ‘soft deletes’. The user that has to be removed remains in the database, but a ‘delete timestamp’ is filled in. This way, the user may be reactivated at a later moment, and also all information about this user is still available in the database.
Before saving information that is submitted by the user himself into a database, the information has to be validated. Validation is important to protect your application against SQL injection. Besides safety concerns, information also needs to be ‘correct’: if a visitor of your website subscribes to an email newsletter, you have to verify that its a real email address
The Laravel framework contains an easy to use validator class for this purpose. For each input field its requirements can be specified. Is it a required field? How many characters should the field contain at least? Is it a date in a certain format? There are plenty of options to quickly validate all user input before storing the data to your database.
This was the second blog post about the use of Laravel for the development of webapplications. Even though this blog only contains some short examples to illustrate the possibilities of the Laravel framework, we are very enthusiastic about this relatively new php framework. It has been of great help during the development of our latest webapplication, because it optimized our workflow and made the programming very efficient. We will definitely keep using Laravel for future applications.