Hello,
Today, I’ll try to tell about a little tiny trick about PHP traits using Laravel as example.
I’ll rename a private method in PHP traits and use it like a public method.
There is an awesome package for Laravel called Eloquent-Sluggable created by Colin Viebrock, which I’ve been using since Laravel 4.
I couldn’t find a similar article for the same purpose with this package, so I decided to write a simple blog post.
Today, in a project, I needed to create the slug before saving. First, I thought of running the method directly in a model, so I tried running
Model::makeSlugUnique('my-slug-that-may-not-be-unique');
But as expected, it failed, because its property was protected
, and I should not fetch non static method statically.
So I tried this as a second attempt: I’ve changed the use SluggableTrait;
trait line Model.php
like this:
use SluggableTrait { makeSlugUnique as public; }
But it failed as expected.
So I’ve decided to rename the method. This is what I did:
use SluggableTrait { makeSlugUnique as uniqueSlug; }
and When I run this,
Model::uniqueSlug('my-slug-that-may-not-be-unique');
It returns me my-slug-that-may-not-be-unique-1
with a suffix and separator that respects my settings that I’ve set in model.