DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Databases
  4. What Is Laravel and Why Should You Care?

What Is Laravel and Why Should You Care?

If you haven’t heard of Laravel before and you’ve been developing websites and web applications, then I’m about to rock your world.

Łukasz Mądrzak user avatar by
Łukasz Mądrzak
CORE ·
Nov. 27, 18 · Opinion
Like (6)
Save
Tweet
Share
13.64K Views

Join the DZone community and get the full member experience.

Join For Free

Laravel is a PHP framework that will drastically improve your productivity. It has been around for quite some time. The first version was released in June of 2011. However, the founder, Taylor Otwell, and his crew are actively improving it.

The latest version (5.7), only came out in September of 2018. It runs on PHP 7.1.3 and above. It follows the MVC (Model-View-Controller) pattern which allows for a much-needed separation of concerns in our web applications.

I will describe a few of the most important features present in Laravel and hopefully, I will convince you to give it a try. There's nothing to lose (except maybe one of your evenings filled with Netflix) and so much to be gained.

Features

Laravel’s most important features (in my opinion) are as follows.

Eloquent ORM (Object-Relational Mapping)

PHP’s implementation of the active record pattern. In simple terms, this pattern allows us to present database tables as classes. Instances of this class are tied to each row in the table. A bit confused? All this means is that you can define a class, e.g. User, which will be tied to the “users” table in your database. Each row in the “users” table is then represented as an instance of the User class. Simple.

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Then to find a user in the database you can simply call User::find(1); where 1 is the unique id of the row in the users table. Eloquent obviously allows for more complex select queries.

Views With Blade

Views contain all of the HTML “code” served by your application. That way you don’t ever have to combine the business logic with the presentation layer. Views support Blade templating engine which allows you to write reusable pieces of HTML code as well as generic layout files. It becomes very useful when certain pages of your application consist of the same components, e.g. a footer. Here’s an example, as today we’re all about examples:

@extends('layouts.app')

@section('title', 'Page Title')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

It's important to note that Blade templates must be saved with a .blade.php extension.

Controllers

Controllers allow for the grouping of related request handling logic into a single class.

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return View
     */
    public function show($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

Routing

A very simple routing mechanism with all the routes conveniently listed in one file is present. You can naturally have multiple files if you wish, especially if you have a bigger application that supports an API. Here’s an example of a route definition.

Route::get('profiles', ‘UserController@getProfiles');

Then you can use this route anywhere in any of your HTML forms (using Blade).

<form method="GET" action="/profiles">
    @csrf
    ...
</form>

Upon submission of this form, the routing mechanism will recognize the route and execute the code specified in the definition. In this case, Laravel will look for the UserController class and the getProfiles method.

As a bonusl, by adding the@csrf annotation to the form, you protect your system from Cross-Site Request Forgery (CSRF) attacks.

Simple Authentication

Have you implemented a complete authentication mechanism with registration and login and then forgotten the password functionality in 10 seconds? No problem. Just run those two simple commands: 

  • php artisan make:auth

  • php artisan migrate 

Most web applications must implement an authentication system but why reinvent the wheel? With Laravel it comes for free.

Conclusion

There are many benefits to implementing web applications with Laravel but first and foremost it will save you a lot of time.

The key features I've highlighted today are as follows:

  • Eloquent ORM

  • Blade Templating

  • Controllers

  • Nice Routing

  • Super quick authentication mechanism.

But there's so much more!

A huge amount of resources for beginners and a kick-ass documentation is also at your disposal. An amazing community of developers should also be mentioned. You can get started with Laravel and find out more here.

Happy coding!

Database Laravel

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Develop a Portrait Retouching Function
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • The Future of Cloud Engineering Evolves

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: