Skip to content

Pivot tables? #337

Answered by jonjakoblich
kennymeyers asked this question in Q&A
Jan 28, 2023 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

@kennymeyers, @moeen-basra
I implemented the following solution with pivot data on a many-to-many relationship. Instead of using a DataCollection of RoleData I created a new data object called UserRoleData to make the pivot data more accessible. I then added that to my UserData object.

My model

class User extends Model
{
    public function roles(): BelongsToMany
    {
         return $this->belongsToMany(Role::class)
            ->withPivot('field_a', 'field_b');
    }
}

UserData

use App\Models\User;
use Spatie\LaravelData\Data;

class UserData extends Data
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
        public readonly ?s…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@kennymeyers
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by kennymeyers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants