Hackernoon logoWhy Laravel Is a Great Choice For MVP by@lukapg

Why Laravel Is a Great Choice For MVP

Luka Abazovic Hacker Noon profile picture

@lukapgLuka Abazovic

Laravel/VueJS developer. I enjoy building side projects.

Over the last few years, I took part in creating several startup projects, all of which involved building an MVP (Most Viable Product) as fast as possible. Different projects meant that I worked with various technologies while working on them, including Ruby on Rails, NodeJS and CodeIgniter. All those technologies are great and ultimately did the job but I've always felt like the basic stuff could be done a lot easier and that I should find something which would allow me to prototype faster.

Enter Laravel. A PHP framework designed around the very idea of setting the foundation so that one may focus on building the core of the product. Being a PHP framework was perfect for me, as I had extensive experience with the language already but PHP was not my go-to language for my projects. All of that changed with Laravel. I'm going to outline several main reasons why one should consider Laravel framework when building a web application MVP.

User authentication out of the box

Laravel ships with pre-configured user authentication out of the box. This is perfect for situations when you just want to test an idea out, and not spend days wiring up an authentication system. This means you get all the basic authentication features - user registration, login, e-mail verification, password recovery etc.

Maintaining the details about the currently logged in user can be cumbersome in classic PHP. With Laravel you can access the details about the current user anywhere in your application, as simple as this:

$user = Auth::user();

Laravel also offers the Socialite package, which allows you to authenticate users using different social networks and webistes, such as Facebook, Twitter, Github, Google etc.

Simple routing

The way Laravel handles routing is clean and straightforward. Want to create routes for your web application? They are all contained in a single web.php routes file. Interested in setting up API routes? You guessed it, api.php is the place to go. A particularly useful feature of Laravel routing is grouping. Consider, for example, a scenario in which you would want some pages of your application to be accessible only to authenticated users of your website. Instead of defining them all separately, all these page routes can easily be grouped by using the Auth middleware, which ensures they are only accessible to authenticated users:

Route::group(['middleware' => 'auth'], function () {
    Route::get('/settings', function ()    {
        // Uses Auth Middleware
    });
    Route::get('/profile', function () {
        // Uses Auth Middleware
    });
});

Eloquent ORM

Laravel's ORM (Object Relational Mapping) is called Eloquent and it's very powerful. It makes all database operations you need to perform in your application a breeze. As Laravel is based on an MVC (Model-View-Controller) architecture, when using Eloquent, each database table has a corresponding Model (the M in MVC) that is used to interact with that table. Let's consider an example of getting all the records from the invoices table (represented by the Invoice model) in our application:

$invoices = Invoice::all();

As you can see, very simple. Using query builder, you can chain together different methods in order to run more complex queries:

$invoices = Invoice::where('active', 1)
               ->orderBy('total')
               ->take(10)
               ->get();

Query builder's interface is very intuitive and fluent, so you can have a basic understanding of the query even without much knowledge of SQL syntax. In the example above, we select all invoices which are active, order them by the field total, take only the first 10 records and then retrieve those records in a Laravel Collection by using the get method.

Laravel Collection is another fantastic feature of the framework that you can read more about. It provides us with a wrapper around the classic PHP array type which allows us to work with arrays easily. It is particularly useful when combined with Eloquent. Using our previous example, say that we wanted to remove all invoices which were paid from our selection; essentially filtering them to include only unpaid invoices. We could use Collection's reject method:

$activeInvoices = $invoices->reject(function ($invoice) {
    return $invoice->paid;
});

Like I mentioned before, the intuitive interface that Eloquent provides us for working with databases is simply a joy to use and highly convenient when you need to focus on building an MVP fast and not delve deep into SQL syntax.

Blade templating engine

Laravel allows you to build you views efficiently and quickly using it's very own Blade templating engine. Blade comes packed with useful directives and functionalities, enabling you to rapidly prototype pages for your application without writing complex presentation logic that would be required without these features. For example, say you need to display different messages on your page for guests and authenticated users. You could use Blade's auth and guest directives:

@auth
    <p>Welcome to the dashboard!</p>
@endauth

@guest
    <p>Please sign in</p>
@endguest

Blade enables us to easily use loops in our views as well. Iterating over arrays is easy enough:

@foreach ($invoices as $invoice)
    <p>Invoice number {{ $invoice->id }}</p>
@endforeach

Need to display a message when the array is empty? This is a perfect scenario for Blade's unique forelse directive. You can iterate over the array and, using the empty directive, display content in case the array is empty:

@forelse ($invoices as $invoice)
    <li>{{ $invoice->title }}</li>
@empty
    <p>No invoices yet!</p>
@endforelse

Blade also allows us to create master-child views, define sections which child views can extend, define reusable components which can be used in many views and much more. Such a templating engine is a perfect tool for creating your presentation layer as quickly as possible.

Rapid e-mail integration

When building an MVP, especially if it's a SaaS product, e-mail integration through a transactional e-mail service is a must from the get go. Laravel provides a simple email API for sending e-mails with ready made drivers for Mailgun, Postmark, Amazon SES or a custom SMTP server. All you need to do to get started is fill out the configuration details. Also, Laravel allows you to easily configure Mailtrap, a service for testing your application emails, before you start using a paid service in production.

Besides enabling us with ease of connecting to various e-mail providers, Laravel also equips us with Mailables, a feature which allows us to define every type of e-mail we use in our applications. Using Mailables, we can define view templates in Laravel, just as we would for any page, and hook it up to be used as an e-mail template. Every Mailable is created by using the make:mail command:

php artisan make:mail InvoicePaid

In this example, we would like to define an e-mail which should be sent as a thank you message to a customer every time an invoice is paid. Mailable features an intuitive interface which allows us to chain methods, much like with Eloquent and define exactly what that e-mail type should do. In the InvoicePaid class generated for us, inside the build method, we can set up the e-mail action:

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->from('example@example.com')
                ->view('emails.invoice.paid')
		->attach('/path/to/file');
}

So, every time an invoice is paid, Laravel will send an e-mail with the template defined by the view method, using an address defined by the from method, with an attachment defined by the attach method. Quite straightforward. Now, every time we wish to send this e-mail type, we can do it from the controller:

Mail::to($email)->send(new InvoicePaid);

These are just the very basics. With more advanced features, Mailables can be hooked up to be called on specific events, use dynamic data, various template formats and more.

Sanctum

Often, we need to build a SPA (single page application) for our Laravel API backend. The most common problem when building a SPA is building a token-based authentication system for it. Luckily, Laravel package called Sanctum provides a featherweight authentication system for SPAs and mobile applications.

Sanctum offers a simple way to authenticate single page applications that need to communicate with our Laravel APIs. Sanctum does not use tokens of any kind. Instead, Sanctum uses Laravel's built-in cookie based session authentication. This basically means that you can use the standard Laravel authentication system to authenticate users in your SPA without any hassle of storing and refreshing tokens on the client side. A kind of magic and a huge time saver when building an MVP!

Vibrant ecosystem

The Laravel ecosystem is, perhaps, the richest environment of any language of framework. So many official packages and tools for various tasks allow us, the developers, to save so much time when building products and focus on the important stuff. Just to name a few of the most popular Laravel packages and tools:

  • Cashier - Subscription billing integration (integrate Stripe in your application easily)
  • Socialite - Authenticate users using various social networks and websites
  • Dusk - Browser testingTelescope - Debug assistant
  • Echo - Realtime events
  • Scout - Add full-text search in your application

And so much more. With Laravel, virtually every major application design need a developer may face is provided for by an official package.

Documentation & community

Last, but certainly not least, Laravel features a documentation which is among the best among any language or framework. Every single aspect and language feature is described and showcased in the documentation, with common use cases outlined in a clear and understandable fashion. It is continuously updated with every major Laravel version and only gets better.

The community of Laravel developers is big and gets bigger every day as the framework is currently the most popular PHP framework out there. Hundreds of the most often questions and problems are already solved in various online communities and forums. You will get help with whatever you get stuck with.

With all this in mind, I created an online resource which, hopefully, gives some value to the community of Laravel (and soon to be) developers. When trying to create a great product, you are always looking for ways to improve the performance and efficiency of your code. While great tutorials are available on many websites, what I found lacking was a single place for short and simple Laravel tips, which, essentially, advise on ways your Laravel code could be better, and more efficient in small bits. Thus, the website is called LaravelBit. Practical and small bits of Laravel code, published 3x per week. And completely free.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.