lundi 9 juillet 2018

How to get value from child table using eloquent in Laravel?

I have two table : users and profile. A user may have one or multiple profile. I want to access the combined data of both table: 'ID' in user table is foreign key in profile table

User model :

class User extends Authenticatable
{
   use Notifiable;

   protected $fillable = [
    'first_name','last_name', 'email', 'password','phone','user_type',
   ];

   protected $hidden = [
    'password', 'remember_token',
   ];

   public function profile()
   {
       return $this->hasMany(profile::class);
   }
}

Profile model is :

class profile extends Model
{
   protected $fillable = [
                    'id','relationship_status','dob','height',
                    'weight','primary_language'
                  ];
   protected $primaryKey = 'profile_id';

   public function User()
   {
     return $this->belongsTo(User::class,'id','id');
   }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire