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
Do you have 'athlete_id' declared in the model?
Double check that the two foreign id fields are fillable on your model.
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
ah, yea, your relationships aren't correct if you want to have multiple atleta's attached.
how to resolve this?
https://github.com/kinzinho666/rkmineiro
GitHub
GitHub - kinzinho666/rkmineiro
Contribute to kinzinho666/rkmineiro development by creating an account on GitHub.
Equipes athletas should be a hasMany not a belongsTo
Check out the laravel docs for relationships to get a better understanding.
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
"atleta_id" not show on query
Sorry not seeing anything obvious. Try debugging with the filament lifecycle hooks to see why the form is empty.
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