#Save builder data in HasMany Relationship

6 messages · Page 1 of 1 (latest)

opal plume
#

Hello everyone,

I’m starting this conversation to discuss one of my needs.
In one of my projects, I needed to use a Builder component but store the result via a HasMany relationship instead of the traditional JSON approach.

So, I packaged this solution here: https://github.com/uni-deal/filament-relationship-builder

As @sleek schooner suggested, I’d love to hear your thoughts on the approach I used (which I partly borrowed from the Repeater component).

I was wondering if this need comes up often—maybe it could even be integrated into Filament itself at some point (though that’s not up to me, lol).

I’m open to any suggestions or feedback regarding the approach, the code, or the single RelationshipBuilder.php file.

Looking forward to your thoughts! 🚀

GitHub

Registers a FilamentPHP builder via an Eloquent relationship with order, type, and data columns. - uni-deal/filament-relationship-builder

spark siren
#

how do I exactly use this?

do I need to add type, data and order column on my model?

after I press the submit button, everything except id and both model's foreign id are just null

spark siren
#
models/User.php

class User extends Model
{
    public function UserBooks()
    {
        return $this->hasMany(UserBook::class, 'user_id', 'id');
    }
}
models/UserBook.php

class UserBook extends Model
{
    protected $fillable = [
        'user_id',
        'book_id',
        'column_a',
        'column_b',
        // ...
    ];

    protected $casts = [
        'data' => 'array',
    ];

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

    public function book()
    {
        return $this->belongsTo(Book::class, 'book_id', 'id');
    }
}
UserResource.php

RelationshipBuilder::make('user_books')
   ->label('User Books')
   ->addActionLabel('Add Book')
   ->relationship('userBooks')
   ->blocks([
      Block::make('type_a')
         ->label('Block Type A')
         ->schema([
            TextInput::make('column_a')
         ]),
      Block::make('type_b')
         ->label('Block Type B')
         ->schema([
            Textarea::make('column_b'),
         ])
   ])
   ->minItems(1)
   ->blockNumbers(false),
spark siren
spark siren
#

like this, @opal plume

#

sorry for the ping, feel free to ignore if you're still busy