Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

makapaka's avatar

How do I load 3rd level relationship for user in SparkServiceProvider

I am successfully loading a relationship in SparkServiceProvider as per docs

 Spark::swap('UserRepository@current', function() {
    $user = Auth::user();
    if ($user) {
        $user->load('typable');
    } 
    return $user;
  });

Thats fine, it works, however I now need to load another level on the above typable relationship.

So a User has typable_id, and typable_type, with example type of student. That loads the student model in, and now on the student model there is a polymorphic relationship to tags, again, taggable_id, taggable_type.

In other words, a User is a Student, and Student hasMany tags.

Ok so how should I be accessing this relationship in the SparkServiceProvider above ? Obviously $user->load('taggable') is incorrect.

0 likes
0 replies

Please or to participate in this conversation.