Edit is not populating the Repeater with the appropriate relationship.
Hi there! I'm currently working on building a client resource that requires adding multiple vehicles. To achieve this, I created a DB table for store the vehicles and used the hasMany relationship with my client model. The create action works perfectly, allowing me to add multiple vehicles using the repeater input. However, when I try to edit a previously created client who already has some vehicles associated with them, all the fields are filled correctly except for the repeater. The repeater doesn't show the previously added vehicles but still allows me to add new ones. Any suggestions on how to fix this would be greatly appreciated.
15 Replies
my repeater
Repeater::make('veiculos') ->relationship() ->addAction(fn (Action $action) => $action->label('Adicionar outro veículo')) ->schema([ Forms\Components\TextInput::make('nome')->required(), Forms\Components\TextInput::make('placa_principal') ->alphaNum() ->mask('**') ->rule('max:7') ->required(), Forms\Components\TextInput::make('placa_secundaria') ->alphaNum() ->mask('**') ->rule('max:7'), ]) ->columns(3),and my model client
class Cliente extends Model
{
protected $guarded = [];
public function veiculos()
{
return $this->hasMany(VeiculoCliente::class);
}
}
check the db and compare the previously added records and compare them to the new ones,,, is the keys correct...
I recreate the DB so many times, and I still can create new vehicles, but they can't appers to be edited
after create them are they stored correctly in the db?
yes, btw they have the IDs of the relationships that I expected
Isn't it strange? I tried to find some examples of the repeater being used, but I didn't find much. I thought that it actually didn't have an editing function, but then I came across a mention of it in the documentation. It stated that, yes, the repeater can be edited and changes the values in the related table, which is exactly what I was hoping for.
ya very strange, your code look good to me
I have a repeater in another one and they working fine
https://github.com/lara-zeus/bolt/blob/47f943367417dbbcce25f6bbf78579e7343596ef/src/Concerns/Schemata.php#L45C21-L45C21
GitHub
bolt/src/Concerns/Schemata.php at 47f943367417dbbcce25f6bbf78579e73...
form builder for your users, with so many use cases - lara-zeus/bolt
check your other form components, something could be affecting something
also in edit.. do you have any custom queries or any other logic in the model could prevent getting the data of the repeater
in the client model no, but my model of the vehicle here:
class VeiculoCliente extends Model
{
protected $table = 'veiculos_clientes';
protected $fillable = ['nome', 'placa_principal', 'placa_secundaria'];
public function cliente()
{
return $this->belongsTo(Cliente::class);
}
}
test this in the Repeater!! ....
->relationship('veiculos')
maybe something in my migrations?
public function up(): void
{
Schema::create('veiculos_clientes', function (Blueprint $table) {
$table->id();
$table->string('nome', 255)->nullable(false);
$table->string('placa_principal', 7)->nullable(false);
$table->string('placa_secundaria', 7)->nullable();
$table->foreignIdFor(\App\Models\Cliente::class)->constrained();
$table->timestamps();
});
}
tested, nothing newlooks good
are you in a resource or custom page?
in resource
I test dump the data from ->mutateRelationshipDataBeforeFillUsing(), and the dump was not called, so I assume that the fill event its not happening for the repeater
I think it means the repeater didnt find the relation
can you test in a route or any other page, to get the client with it's Veiculo to make sure the relation is good
It was a good idea, tested in a route calling dd(\App\Models\Cliente::first()->veiculos()->first()->get());
and returns the model filled
I redo my code using a json colunm instead a relation, its sad but works, thanks for you help @Lara Zeus