Python
PyCharm and WSL
Over the past few months, I’ve been monitoring a ticket closely. Over the course of two years, the ticket has accrued over 130 votes. It’s the one about WSL support in PyCharm, and by extension, Read more…
Over the past few months, I’ve been monitoring a ticket closely. Over the course of two years, the ticket has accrued over 130 votes. It’s the one about WSL support in PyCharm, and by extension, Read more…
Its built-in stylus is useful and enjoyable, but the Stylo 6 is too slow to recommend Continue reading…
An operator works during the mooring of an undersea fiber optic cable at Arrietara beach near the Spanish Basque village of Sopelana on June 13, 2017. | Photo by Ander Gillenea / AFP via Getty Read more…
Posted by Nick Grayson, Product Manager Android works best when it helps developers create apps that people love. That’s why we are dedicated to providing useful APIs like Activity Recognition which, with the user’s permission, Read more…
Amazon Music is finally coming to Google’s set-top box operating systems, with a new app rolling out for Google TV (currently just available on the latest Chromecast) and Android TV starting today. The app will Read more…
Posted by Erica Hanson, Global Program Manager, Google Developer Student Clubs Google Developer Student Clubs, a program of university based community groups for students interested in Google developer technologies, recently started hosting study groups called Read more…
[AdSense-A]
We've released a new package called spatie/laravel-prefixed-ids. In this post, I'd like to tell you all about it.
When thinking of companies with excellent APIs, Stripe is one of the first that comes to mind. They have excellent docs, and it seems like they have polished every detail to make sure the developer experience is just right.
One of the details they took care of is prefixing all of their ids. Here's an example: when creating a token to interact with their API, you'll notice that that token is prefixed. Let's take a look at such a token:
sk_test_FKdleMEKfiemsiDSMEODL
In the example above, the sk stands for "secret key", and test signifies that this token is meant to be used in Stripe's test environment. Without that prefix, the value would just be a random string of which you'd have to guess what it is.
In fact, Stripe prefixes almost all of their ids. In my mind, this is a great little detail.
The spatie/laravel-prefixed-ids can automatically generate prefixed ids in a Laravel app. It offers handy methods to retrieve Eloquent models using those prefixed ids.
You can install the package via Composer. No surprises there.
composer require spatie/laravel-prefixed-ids
On each model that needs a prefixed id, you should use the SpatiePrefixedIdsModelsConcernsHasPrefixedId trait.
namespace AppModels;
use Illuminate<span class="hljs-title">Database<span class="hljs-title">Eloquent<span class="hljs-title">Model;
use Spatie<span class="hljs-title">PrefixedIds<span class="hljs-title">Models<span class="hljs-title">Concerns<span class="hljs-title">HasPrefixedId;
class YourModel extends Model
{
use HasPrefixedId;
}
You'll need to write a migration to add a prefixed_id column to its underlying table.
If you wish to use another attribute name, you should publish the config file (see below) and set the prefixed_id_attribute_name config value to the attribute name of your liking.
Schema::create('your_models_table', function (Blueprint $table) {
$table->string('prefixed_id')->index();
});
To register your models, you should pass the desired prefix and your model's class name to PrefixedIds::registerModels.
SpatiePrefixedIdsPrefixedIds::registerModels([
'your_prefix_' => YourModel::class,
'another_prefix' => AnotherModel::class,
]);
Typically, you would put the code above in a service provider.
With this in place, when a model is created, it will automatically have a unique, prefixed id in the prefixed_id attribute.
$model = YourModel::create();
$model->prefixed_id // returns a random id like your_model_fekjlmsme39dmMS
You can find the model with a given prefix by calling findByPrefixedId on it.
YourModel::findByPrefixedId('your_model_fekjlmsme39dmMS'); // returns an instance of YourModel
YourModel::findByPrefixedId('non-existing-id'); // returns null
You can call find on SpatiePrefixedIdsPrefixedIds to automatically get the right model for any given prefixed id.
$yourModel = SpatiePrefixedIdsPrefixedIds::find('your_model_fekjlmsme39dmMS'); // returns an instance of YourModel or null
$otherModel = SpatiePrefixedIdsPrefixedIds::find('other_model_3Fjmmfsmls'); // returns an instance of OtherModel or null
To use the prefixed ids in your routes, you'll have to add the getRouteKeyName method to your model. It should return the name of the attribute that holds the prefixed id.
public function getRouteKeyName()
{
return 'prefixed_id';
}
With this in place a route defined as...
Route::get('your-model/{yourModel}', YourModelController::class)`
... can be invoked with an URL like "/your-model/your_model_fekjlmsme39dmMS".
You'll find more info on route model binding in the Laravel docs.
When creating an API, consider using prefixed ids. The consumers of your API will probably much appreciate this.
spatie/laravel-prefixed-ids isn't the first package our team has created. Be sure to take a look at this list of PHP and Laravel packages our team has created previously.
(more…)The basic TV option reportedly appears at setup. | Image: Google / 9to5Google Google TV, the search giant’s latest attempt at TV software, will include a new “Basic TV” mode that strips out a TV’s Read more…
The PUBG universe is expanding with a new mobile title for iOS and Android called PUBG: New State, which catapults the battle royale ahead in time for a more futuristic take on the massive multiplayer Read more…
Illustration by Alex Castro / The Verge The Australian government has passed a new law requiring Google and Facebook to negotiate with news outlets to pay for their content or face arbitration. “This is a Read more…