PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
No Result
View All Result
Home Web Dev

Squire: Static Eloquent Model Library

February 9, 2021
in Web Dev
277 15
Squire: Static Eloquent Model Library


Squire is a library of prebuilt static Eloquent models for common fixture data. It provides information about countries, currencies, airports, and more to your Laravel app, without the use of a third-party API.

Setup is simple – just install the model packages that you need in your app, and you’re ready to go. Squire comes out of the box with queryable Eloquent models, validation rules, and full localization support.

Squire is built upon the principles demonstrated in Caleb Porzio’s Sushi package: all models are served by their own SQLite databases, and cached.

Building a Country Select Input

We’re going to create a country select input using data from Squire. Let’s install the SquireModelsCountry model in English:

composer require squirephp/countries-en

Tip: all Squire models are translatable. Squire can automatically serve the correct data based on your user’s locale. To try it out, also install the squirephp/countries-fr package, and notice how French users will be served in the correct language.

To keep things simple, use Blade to query the Country model and render our select options:

<select name="country">
    @foreach (SquireModelsCountry::orderBy('name')->get() as $country)
        <option value="{{ $country->id }}">{{ $country->name }} {{ $country->flag }}</option>
    @endforeach
</select>

Validate the input to ensure that a valid country has been selected in a controller:

use SquireRulesCountry;

$request->validate([
    'country' => ['required', 'string', new Country],
]);

You’re able to use conventional Eloquent relationships between Squire and non-Squire models. Let’s set up a country relationship for the country_id column on the AppModelsUser model:

use SquireModelsCountry;

public function country()
{
    return $this->belongsTo(Country::class);
}

Save the selected country to $user in your controller:

$user->country()->associate($request->country);

$user->save();

Rounding Off

Squire has the capability to replace many third-party APIs in your Laravel app, cutting costs and improving performance.

The ecosystem is constantly expanding as new models and translations are submitted and published by the community. If you see anything missing, feel free to open a feature discussion or pull request in the repository, or release your own Squire package.

If you have any questions, feel free to tweet me @danjharrin and I’ll do my best to respond. If you love Squire, sponsoring me on GitHub ensures I can dedicate time to maintaining and improving the package.





Source link

Share219Tweet137Share55Pin49

Related Posts

Web Components Are Easier Than You Think
Web Dev

Web Components Are Easier Than You Think

When I’d go to a conference (when we were able to do such things) and see someone do a...

March 8, 2021
Going Beyond The Basics — Smashing Magazine
Web Dev

Obscure Mobile Design Techniques That Boost UX — Smashing Magazine

About The AuthorGert Svaiko is a professional copywriter and mainly works with digital marketing companies in the US and...

March 8, 2021
Invoker–the no-bull Laravel tool | Laravel News
Web Dev

Invoker–the no-bull Laravel tool | Laravel News

Invoker by Beyond Code lets you access all your Laravel applications with an instant admin panel—locally, via SSH and...

March 8, 2021
Laravel Tutorial: Step by Step Guide to Building Your First Laravel Application
Web Dev

Laravel Tutorial: Step by Step Guide to Building Your First Laravel Application

Since its initial release in 2011, Laravel has experienced exponential growth. In 2015, it became the most starred PHP...

March 7, 2021
Next Post
New Laravel Route “Missing” Method

New Laravel Route “Missing” Method

How to Add Fingerprint Login in Ubuntu and Other Linux

How to Add Fingerprint Login in Ubuntu and Other Linux

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Community Resources, Weekly Newsletter, And Boosting Skills Online — Smashing Magazine

Community Resources, Weekly Newsletter, And Boosting Skills Online — Smashing Magazine

September 1, 2020
Bring your C#.NET skills to Amazon SageMaker : idk.dev

Bring your C#.NET skills to Amazon SageMaker : idk.dev

June 23, 2020
Some Recent Videos About Websites That Are Pretty Good

Some Recent Videos About Websites That Are Pretty Good

January 5, 2021
How to Create a Shrinking Header on Scroll Without JavaScript

How to Create a Shrinking Header on Scroll Without JavaScript

February 16, 2021

Categories

  • AWS
  • Big Data
  • Database
  • DevOps
  • IoT
  • Linux
  • Web Dev
No Result
View All Result
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In