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

sezertunca's avatar

Custom Pivot Model returns null

Hi everyone and @JarekTkaczyk especially. I've followed your article on Custom pivot models in Eloquent but I'm having some trouble when playing around in tinker.

I'm trying to add SoftDeletes so that pivot data isn't lost, just hidden so to speak.

I have two models, User and Assignment. Previously they were both using a belongsToMany relationship which was working fine but as stated above, we'd like to add some extra functionality.

User Model

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

/**
 * @param Model $parent
 * @param array $attributes
 * @param string $table
 * @param bool $exists
 * @return AssignmentUserPivot|\Illuminate\Database\Eloquent\Relations\Pivot
 */
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
    if ($parent instanceof Assignment) {
        return new AssignmentUserPivot($parent, $attributes, $table, $exists);
    }

    return parent::newPivot($parent, $attributes, $table, $exists);
}

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

/**
 * @param Model $parent
 * @param array $attributes
 * @param string $table
 * @param bool $exists
 * @return AssignmentUserPivot|\Illuminate\Database\Eloquent\Relations\Pivot
 */
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
    if ($parent instanceof User)
    {
        return new AssignmentUserPivot($parent, $attributes, $table, $exists);
    }

    return parent::newPivot($parent, $attributes, $table, $exists);
}

AssignmentUser Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AssignmentUser extends Model
{
    protected $table = 'assignment_user';

    protected $fillable = [
        'assignment_id',
        'user_id',
        'revised_hand_out_date',
        'revised_hand_in_date',
        'assignment_content',
        'date_completed',
        'mark',
        'feedback',
        'feedback_date'
    ];

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

    public function assignment()
    {
        return $this->belongsTo(Assignment::class);
    }
}

AssignmentUserPivot Pivot

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;

class AssignmentUserPivot extends Pivot
{
    protected $dates = ['completed_at'];
}

When I run php artisan tinker

>>> $user = User::first(); // returns an instance of a User
>>> $assignment = Assignment::first(); // returns an instance of an Assignment

When I try to get a User's first Assignment

>>> $user->assignments->first(); // I expect to get an instance of Assignment but instead get AssignmentUser

And then of course, when I call $assignment->pivot it returns null as I've already got the pivot, not the Assignment.

0 likes
1 reply
bashy's avatar
$user->assignments()->first();

?

Please or to participate in this conversation.