Skip to content

[Bug]: Trying to load belongsToMany relation with pivot table always empty #2285

@csifalmeria

Description

@csifalmeria

What happened?

Similar to issue #2213 I am trying lo create a table to show a list of users having a column wiht their roles. Relation uses a pivot table.

User class seems like:

class User extends Model
{
	protected $table = 'users';

	protected $casts = [
		'email_verified_at' => 'datetime'
	];

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

	protected $fillable = [
		'name',
		'email',
		'email_verified_at',
		'password',
		'remember_token'
	];

	public function roles()
	{
		return $this->belongsToMany(Role::class, 'model_has_roles', 'model_id', 'role_id');
	}
}

Calling User::find(1)->roles from tinker returns user roles

Trying to get from the column it is always empty.

Table builder method is:

 public function builder(): \Illuminate\Database\Eloquent\Builder
    {
        return User::query()->with(['roles']);
    }

Column definition for testing:

            Column::make('Roles')
                ->label(function ($row): string {
                    return print_r($row, true);
                }),

How to reproduce the bug

No response

Package Version

3.7.3

PHP Version

8.2.x

Laravel Version

12.19.3

Alpine Version

No response

Theme

None

Notes

No response

Error Message

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions