F
Filament2mo ago
KinBH

Salve multiple relation

I have simple form: Forms\Components\Select::make('competicoe_id') ->label('Competição') ->relationship(name: 'competicoes', titleAttribute: 'titulo') ->searchable() ->preload() ->required(), Forms\Components\Select::make('atleta_id') ->label('Atletas') ->relationship(name: 'atletas', titleAttribute: 'nome') ->searchable() ->multiple() ->preload() ->required(), My team table: Schema::create('equipes', function (Blueprint $table) { $table->id(); $table->timestamp('created_at')->nullable(); $table->timestamp('updated_at')->nullable(); $table->timestamp('deleted_at')->nullable(); $table->unsignedBigInteger('atleta_id'); $table->unsignedBigInteger('competicoe_id'); $table ->foreign('atleta_id') ->references('id') ->on('atletas') ->onDelete('cascade') ->onUpdate('cascade'); $table ->foreign('competicoe_id') ->references('id') ->on('competicoes') ->onDelete('cascade') ->onUpdate('cascade'); }); Equipe Model: public function atletas() { return $this->belongsTo(Atleta::class, 'atleta_id'); } public function competicoes() { return $this->belongsTo(Competicoe::class, 'competicoe_id'); } Atleta Model: public function equipes() { return $this->hasOne(Equipes::class); } I select a competition and 3 athletes, but when i click o save return error SQLSTATE[HY000]: General error: 1364 Field 'atleta_id' doesn't have a default value INSERT INTO equipes (competicoe_id, updated_at, created_at) VALUES (1, 2024-05-15 15:00:35, 2024-05-15 15:00:35)
9 Replies
HeyWeb
HeyWeb2mo ago
Do you have 'athlete_id' declared in the model?
awcodes
awcodes2mo ago
Double check that the two foreign id fields are fillable on your model.
KinBH
KinBH2mo ago
yes, both: <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Equipes extends Model { use HasFactory; use SoftDeletes; protected $guarded = []; protected $fillable = ['atleta_id', 'competicoe_id']; protected $dates = ['created_at', 'updated_at', 'deleted_at']; public function atletas() { return $this->belongsTo(Atleta::class, 'atleta_id'); } public function competicoes() { return $this->belongsTo(Competicoe::class, 'competicoe_id'); } } if a remove mutiple, its oks, but with multiple give error
awcodes
awcodes2mo ago
ah, yea, your relationships aren't correct if you want to have multiple atleta's attached.
KinBH
KinBH2mo ago
GitHub
GitHub - kinzinho666/rkmineiro
Contribute to kinzinho666/rkmineiro development by creating an account on GitHub.
awcodes
awcodes2mo ago
Equipes athletas should be a hasMany not a belongsTo Check out the laravel docs for relationships to get a better understanding.
KinBH
KinBH2mo ago
nothing change after change hasMany. QLSTATE[HY000]: General error: 1364 Field 'atleta_id' doesn't have a default value: INSERT INTO equipes (competicoe_id, updated_at, created_at) VALUES....
"atleta_id" not show on query
awcodes
awcodes2mo ago
Sorry not seeing anything obvious. Try debugging with the filament lifecycle hooks to see why the form is empty.
KinBH
KinBH2mo ago
I was able to figure out the problem. It was necessary to make the fields nullable because Filament saves null first and then updates, so it was generating the error. With this problem solved, now I have another lol. When I use MULTIPLE os athlets, POST form comes as an array: update teams set athlete_id = ["2","1"], teams.updated_at = 2024-05-17 14:14:31 where id = 6 ... However, the field is a bigInt due to the relationship. How do I make Filament save this array line by line? competicoe_id | atlhet_id
Want results from more Discord servers?
Add your server
More Posts