A library of static Eloquent models for common fixture data.
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 based on the concepts 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 models into your application. Each model is available in a variety of languages, and you need only install the ones you will use.
As an example, you can install the Squire\Models\Country model in English:
You can interact with a Squire model just like you would any other Eloquent model, except that it only handles read-only operations.
php
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:
php
hasMany(User::class);
}
}
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:
Squire includes a validation rule for each model installed in your app. These allow you to easily validate user input to ensure that it matches a record in a specific model.
Rules can be found in the Squire\Rules namespace. To use one, simply construct a new rule class and pass in the model column name that you would like to validate against:
This code will validate the country input against the name column on the Squire\Models\Country model. If the user enters a country that does not exist, a validation error will be thrown.