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

Randy_Johnson's avatar

Relationship not reading correctly

I am trying to grab the student that is linked to the contact but I am just getting the contact. Anybody got any idea why.

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'category',
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

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

    public function appointments()
    {
        return $this->hasMany(Appointment::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'forename', 
        'surname', 
        'email',
        'telephone',
        'address',
        'gender',
    ];

    public function students()
    {
        return $this->belongsToMany(Contact::class, 'contact_students');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;

    public function contacts()
    {
        return $this->belongsToMany(Contact::class, 'contact_students');
    }

    public function course()
    {
        return $this->belongsToMany(Course::class);
    }

    public function activities()
    {
        return $this->belongsToMany(Activity::class);
    }
}
    public function mount()
    {
        $this->students = Auth::user()->contacts[0]->students;
        dd($this->students);
        $this->activities = Activity::all();
    }
        "id" => 21
        "user_id" => 2
        "forename" => "John"
        "surname" => "Doe"
        "email" => "[email protected]"
        "telephone" => "454325345234"
        "address" => "999 Lets Be Avenue"
        "gender" => "M"
        "created_at" => "2022-05-31 07:12:43"
        "updated_at" => "2022-05-31 07:12:43"
        "pivot_contact_id" => 21
0 likes
7 replies
SilenceBringer's avatar
Level 55

@randy_johnson maybe because students method on Contact model linked to Contact?

	public function students()
    {
        return $this->belongsToMany(Contact::class, 'contact_students');
    }

maybe it should be

	public function students()
    {
        return $this->belongsToMany(Student::class, 'contact_students');
    }
1 like
MichalOravec's avatar

In Contact model it should be

public function students()
{
    return $this->belongsToMany(Student::class, 'contact_students');
}
1 like
MichalOravec's avatar

And why the pivot table is not just contact_student? For which reason you added a letter s there?

1 like
Randy_Johnson's avatar

@MichalOravec I have a feeling that this isn't the way it should be done. (not particular classes as mentioned)

try 
        {
            $activity = new Activity;
            $activity->title = $data['title'];
            $activity->description = $data['description'];
            $activity->save();
    
            foreach ($this->resources as $resource)
            {
                $activity_resource = new ActivityResource;
                $activity_resource->activity_id = $activity->id;
                $activity_resource->resource_id = $resource['id'];
                $activity_resource->save();
            }
    
            $this->resources = [];
            $this->reset(['title', 'description']);
        } 
        catch (\Throwable $th) 
        {
            //throw $th;
        }  
Randy_Johnson's avatar

@MichalOravec So I just php artisan make:model ContactStudent --mr and it created the table. I should of took more time reading docs, but basically the website is for a business which I am trying out, and it had to be done for the beginning of June because that's when the phone starts to ring off the hook.

Not to worry, another 15 days and I'm a free man. Quitting the job and flying solo.

Please or to participate in this conversation.