Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Override/Extend Functions of Laravel Packages

Premium
3 min read

Let's get a bit more practical. Quite often, you want to use a Laravel package but slightly change its functionality. You can't edit anything in /vendor because package updates would wipe out your changes, right? So, I will show you what to do, with two examples.


Example 1: Override Relationship Method

Here's a situation we had at our own Laravel Daily. We're using the spatie/laravel-tags package, which is configured by just adding a Trait to our Model:

app/Models/Course.php:

use Spatie\Tags\HasTags;
 
// ...
 
class Course extends Model
{
use HasTags;
 
// ...

Inside that trait, there's a polymorphic relation method:

vendor/spatie/laravel-tags/src/HasTags.php:

trait HasTags {
// ...
 
public function tags(): MorphToMany
{
return $this
->morphToMany(self::getTagClassName(), $this->getTaggableMorphName())
->using($this->getPivotModelClassName())
->ordered();
}
}

But what we needed in our project was to automatically...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…