Skip to main content

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? (30 h 41 min)

You also get:

55 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…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.