Home » Laravel Pennant Up and Running

Laravel Pennant Up and Running

Loved it? Please Share on Social Media

Introduction

Hey, What’s up, everybody? I hope you are doing fine. Today, we will discuss the laravel pennant package. Laravel pennant package is excellent for integrating new features in the application.

In other words, it provides you with the feature flags, A/B test new interface designs, and trunk-based development strategy for your application.

So, in this tutorial, we’ll learn how to use the laravel pennant package on our laravel application and talk about some essential features of the Laravel pennant package.

Laravel Pennant Installation:

To use this package, we’ll use composer to install the package. Please use the command on your terminal.

composer require laravel/pennant

Well, our package installation process is complete. We must publish the package configuration and migration files using the below artisan command.

php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"

Lastly, using the command below, we will migrate our package migration files for database interactions.

php artisan migrate

Defining a Feature Using Laravel Pennant Package:

If you are trying to add a new feature to your application, you must add your feature code inside AppServiceProvider > boot method. Please check out the code snippet below.

<?php
 
namespace App\Providers;
 
use App\Models\User;
use Illuminate\Support\ServiceProvider;
use Laravel\Pennant\Feature;
 
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Feature::define('customer-video-calling', fn (User $user) => match (true) {
            $user->isSubcribedToPremium() => true,
            $user->isHigestPayingCustomer() => false,
        });
    }
}

Defining a Class Based Feature of:

You can also define your feature in a separate class file. You need to use an artisan command to create a new feature file. Please check out the following command.

php artisan pennant:feature HighPayingCustomer
class HighPayingCustomer
{
    public function resolve(User $user): mixed
    {
        return match (true) {
            $user->isSubcribedToPremium() => true,
            $user->isHigestPayingCustomer() => false,
        };
    }
}

Checking Features inside the Controller:

Using an authenticated user makes checking whether your feature is active straightforward. Please see the code snippet below for more.

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Pennant\Feature;
 
class CustomerController
{
      /**
     * Display a listing of the resource.
     */
    public function index(Request $request): Response
    {
        return Feature::active('customer-video-calling')
                ? $this->isSubcribedToPremium($request)
                : $this->isHigestPayingCustomer($request);
    }
}

Checking Class Based Features:

You can also check the class-based features inside your controller. Please follow the code below for more info.

<?php
 
namespace App\Http\Controllers;
 
use App\Features\HighPayingCustomer;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Pennant\Feature;
 
class PodcastController
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): Response
    {
        return Feature::active(HighPayingCustomer::class)
                ? $this->isSubcribedToPremium($request)
                : $this->isHigestPayingCustomer($request);
    }
}

Conditional Execution With Example:

Another excellent feature is executing your features using the when function by certain conditions. You can check out the code below for more.

<?php
 
namespace App\Http\Controllers;
 
use App\Features\HighPayingCustomer;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Pennant\Feature;
 
class PodcastController
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): Response
    {
        return Feature::when(HighPayingCustomer::class,
            fn () => $this->isSubcribedToPremium($request),
            fn () => $this->isHigestPayingCustomer($request),
        );
    }
}

You can also use the unless function in your application, which is the opposite of the when function.

<?php
 
namespace App\Http\Controllers;
 
use App\Features\HighPayingCustomer;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Pennant\Feature;
 
class PodcastController
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request): Response
    {
        return Feature::unless(HighPayingCustomer::class,
            fn () => $this->isSubcribedToPremium($request),
            fn () => $this->isHigestPayingCustomer($request),
        );
    }
}

Check User using HasFeatures Trait:

You can check your user using the HasFeature trait inside your User.php file.
You can check out the below code for more info.

<?php
 
namespace App\Models;
 
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Pennant\Concerns\HasFeatures;
 
class User extends Authenticatable
{
    use HasFeatures;
}

Check whether the user has features or not using the code below.

if ($user->features()->active('customer-video-calling')) {
    // your logic.
}

Using Blade Directive in Laravel Pennant:

You can use a separate blade directive for your blade file. Please see the below code for more.

@feature('customer-video-calling')
    <!-- 'customer-video-calling' is active -->
@else
    <!-- 'customer-video-calling' is inactive -->
@endfeature

Using Laravel Pennant Middleware:

use Illuminate\Support\Facades\Route;
use Laravel\Pennant\Middleware\EnsureFeaturesAreActive;
 
Route::get('/create/video-call, function () {
    // your logic code
})->middleware(EnsureFeaturesAreActive::using('customer-video-calling'));

Eager Loading With Example:

use Laravel\Pennant\Feature;
 
foreach ($users as $user) {
    if (Feature::for($user)->active('customer-video-calling')) {
        $user->notify(new VideoSessionStarted);
    }
}

Test The Application:

At last, we will start our local server using our artisan command. php artisan serve If there is no problem, our code will work fine, and the project will be running now at http://127.0.0.1:8000/ or http://locahost:8000/. You can know more from their documentation.

Conclusion:

Finished! That’s all about our tutorial. Although this tutorial cannot make you an expert in the Laravel pennant package, it will give you some fundamental knowledge of how to work with this kind of thing in the future. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. If you have questions, suggestions, or tips regarding this post, let us know via our Contact Us page.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.