A library of static Eloquent models for common fixture data.
PHP
Laravel Squire π
Squire is a library of static Eloquent models for fixture data that is commonly needed in web applications, such as countries, currencies and airports. It's built on top of Caleb Porzio's Sushi package.
Common use cases for Squire include:
Populating dropdown options, such as a country selector on an address form.
Attaching extra data to other models in your app, such as airport information to a Flight model. See the section on model relationships.
You can use Composer to install Squire into your application.
composer require danharrin/squire
No additional setup is required.
Using a model
You can interact with a Squire model just like you would any other Eloquent model, except that it only handles read-only operations.
useSquire\Models\Country;
Country::all(); // Get information about all countries.Country::find('us'); // Get information about the United States.Country::where('name', 'like', 'a%')->get(); // Get information about all countries beginning with the letter "a".
Implementing an Eloquent relationship between a model in your app and a Squire model is very simple. There are a couple of approaches you could take.
Using inheritance
The simplest option is to create a new model in your app, and let it extend the Squire model. Your new app model will now behave like the original Squire model, except you can register new methods and customise it to your liking:
See our section on model customisation for more customisation possibilities that are made available if you utilise this method.
Using resolveRelationUsing()
Another option is the resolveRelationUsing() method. This allows you to dynamically register a relationship for a Squire model from anywhere in your app, for example, within a service provider:
useApp\Models\User;
useSquire\Models\Country;
Country::resolveRelationUsing('users', function (Country$country) {
return$country->hasMany(User::class);
});
Column customisation
Squire allows you to customise the column names on any provided model.
Create a new model within your app and let it extend the Squire model that you would like to customise: