laravel pennant example

Laravel Pennant: A Complete Example Guide

By Andrew Wilson
AD

Laravel Pennant, an Eloquent model discriminator for the PHP framework Laravel, is an invaluable tool for handling complex inheritance and type enumeration in your models. This package, developed by BeyondCode, enables you to define a single Eloquent model that can have multiple child types, each with its own unique set of attributes and behaviors. Let's dive into a comprehensive example to understand how Laravel Pennant works.

In this article, we'll explore the installation, setup, and usage of Laravel Pennant using a real-world example: a 'Booking' system with 'Hotel' and 'Restaurant' booking types. By the end, you'll have a solid understanding of how to integrate Laravel Pennant into your projects to manage complex, polymorphic relationships.

Installation and Setup

The first step is to install the Pennant package via Composer. Open your terminal and run the following command:

AD
laravel
laravel
a large poster with many different types of text
a large poster with many different types of text
a large black and white poster with many different languages on it's sides, including the
a large black and white poster with many different languages on it's sides, including the

composer require beyondcode/pennant

Registering the Service Provider

After installing the package, you need to register its service provider in your application's providers array. Open the config/app.php file and add the following line under the 'providers' key:

Laravel
Laravel
Learn Laravel Filament full tutorial: Build powerful admin panels with ease
Learn Laravel Filament full tutorial: Build powerful admin panels with ease
Laravel
Laravel
'providers' => [ // ... BeyondCode\Pennant\PennantServiceProvider::class, ],

Don't forget to run php artisan config:cache to ensure the changes take effect.

Configuring the Package

Next, publish the Pennant configuration files using the Artisan command:

AD

php artisan vendor:publish --tag=pennant-config

a computer screen with icons surrounding it
a computer screen with icons surrounding it
The 2025 Laravel Developer RoadMap
The 2025 Laravel Developer RoadMap
a black background with text that reads,'laravel tip htppdd0 '
a black background with text that reads,'laravel tip htppdd0 '
Laravel feature
Laravel feature
Web design Articles - Page 4 of 22
Web design Articles - Page 4 of 22
Laravel Book : the most popular Laravel book
Laravel Book : the most popular Laravel book

This will publish the pennant.php file in your config directory. You can adjust the default values according to your preferences.

Defining the Booking Model

Now that the package is set up, let's define our main 'Booking' Pennant model. Open your terminal and create a new model using Artisan:

AD

php artisan make:model Booking -pm

The Ultimate Guide to Laravel Development
The Ultimate Guide to Laravel Development

Defining Child Models

Next, create the child models 'HotelBooking' and 'RestaurantBooking'. You can create these without any fillables, as they will inherit from the 'Booking' model:

php artisan make:model HotelBooking -a, php artisan make:model RestaurantBooking -a

AD

Defining the Pennant Model

Now, open the Booking.php file and add the following use statements and Pennant model definition:

use BeyondCode\Pennant\Traits\Pennatable; class Booking extends Model { use Pennatable; protected $discriminator = 'booking_type'; protected $bookableModels = [ HotelBooking::class, RestaurantBooking::class, ]; }

We've set the discriminator column to 'booking_type', and specified the two bookable models.

Modifying the Migration

Finally, open the migration file for the 'bookings' table and add a 'booking_type' column to discriminate between hotel and restaurant bookings:

Schema::create('bookings', function (Blueprint $table) { $table->id(); $table->string('booking_type'); // ... });

The 'booking_type' column will store the type of booking (Hotel or Restaurant).

Using the Pennant Model

Now that our Pennant model is set up, let's see how to use it in our controller:

Creating a New Booking

To create a new booking, we simply create a new instance of the child model and save it:

public function store(Request $request) { $booking = match ($request->booking_type) { 'hotel' => new HotelBooking($request->all()), 'restaurant' => new RestaurantBooking($request->all()), }; $booking->save(); return response($booking, 201); }

Here, we use the PHP 8 match expression to determine which child model to instantiate, based on the 'booking_type' parameter.

Querying Bookings

When querying bookings, you can use the same methods you would with any other Eloquent model. For example, to get all bookings of a specific type:

$hotelBookings = Booking::where('booking_type', 'hotel')->get();

Or, to retrieve a booking by its ID, regardless of its type:

$booking = Booking::find(1);

The Pennant package ensures that the correct child model is returned, based on the 'booking_type' column.

And that's it! You've successfully integrated Laravel Pennant into your project, handling complex inheritance and type enumeration with ease. This powerful tool will save you time and effort in managing polymorphic relationships, allowing you to focus on building innovative features for your application.

Now that you're comfortable with Laravel Pennant, why not explore other packages by BeyondCode to expand your Laravel development toolkit? Happy coding!

AD