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

kenmoini's avatar

Not loading ManyToMany Polymorphic Relationships

Odd thing happening, my M2M Polymorphic relationships are being temperamental. I have the schema setup properly (I'm rather sure of this), and the models as well. I have a few tables, Contacts, Companies, Users and all of these have the M2M relation to the ProfilePicture model. When I load the Contacts and Companies models with the associated ProfilePictures they load just fine. User model not so much. No clue as to why

Contacts Model

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Helpers;

use JeroenDesloovere\VCard\VCard;

class Contacts extends Model
{
    //
    protected $table = 'contacts';
    protected $fillable = [ 'name', 'title', 'notes', 'address1', 'address2', 'city', 'state', 'postalCode', 'country', 'email', 'createdBy', 'status' ];
/**
     * Get the profile picture of the contact
     */
    public function profilePicture() {
        return $this->morphToMany( 'App\ProfilePictures', 'ppictureable' );
    }
}

User Model

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    protected $with = ['profilePicture'];
/**
     * Get this user profile picture
     */
    public function profilePicture() {
        return $this->morphToMany('App\ProfilePictures', 'ppictureable');
    }
}

ProfilePictures Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProfilePictures extends Model
{
    //
    protected $table = 'profilePictures';
    protected $fillable = [ 'url' ];

    /**
     * Get all of the companies that are assigned this picture
     */
    public function companies()
    {
        return $this->morphedByMany('App\Companies', 'ppictureable');
    }
    /**
     * Get all of the contacts that are assigned this picture
     */
    public function contacts()
    {
        return $this->morphedByMany('App\Contacts', 'ppictureable');
    }
    /**
     * Get all of the users that are assigned this picture
     */
    public function user()
    {
        return $this->morphedByMany('App\User', 'ppictureable');
    }

}

ppictureables Schema

-id int(10) unsigned
-profile_pictures_id int(11)
-ppictureable_id int(11)
-ppictureable_type varchar(255)

ppictureables Rows

id   profile_pictures_id  ppictureable_id   ppictureable_type
1    1                    1                 App\User
2    2                    1                 App\Contacts
3    5                    2                 App\Contacts
4    3                    1                 App\Companies

Again, I can query Contacts and ->load('profilePicture') and it loads the data fine, but in the \App\User model it just returns empty even though all the data seems to correlate and be set properly.

public function showCreateProject() {
    $fertilizerProfiles = \App\FertilizerProfiles::select(['name', 'slug', 'id'])->groupBy('name')->get();
    $user = \Auth::user();
    $contacts = $user->contacts;
    $contacts = $contacts->load('company', 'profilePicture');
    $data['fertilizerProfiles'] = $fertilizerProfiles;
    $data['contacts'] = $contacts;
    $collaborators = \App\User::select(['email','name'])->get();
    $data['collaborators'] = $collaborators;
    return view('grow-calculator.create-project')->with('data', $data);
  }

Any thoughts?

0 likes
4 replies
ahuggins's avatar

dont you need on your ProfilePictures model, a method called ppictureable like this:

public function ppictureable()
{
    return $this->morphTo();
}

Basically, your morphedByMany are passing ppictureable, and that needs to be defined somewhere...or maybe you just didn't post it.

kenmoini's avatar

I tried that however nothing changed. Also like I said, the Contacts and Companies models load the pictures just fine but the User model alone does not.

kenmoini's avatar

I also added this to my User model

    protected $morphClass = 'App\User';

However still, no dice. Bout to beat my head bloody against the wall.

kenmoini's avatar
kenmoini
OP
Best Answer
Level 1

Ha, well figured out why it didn't work. Without also selecting the key (id), Laravel/Eloquent had no idea what to bind the tables to. Removed the protected $with from the User model and then in the controller modified to this...

$collaborators = \App\User::with(['profilePicture' => function($query) {
      $query->select('url');
    }])->select(['email','name','id'])->get();

Now works great.

Please or to participate in this conversation.