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?