Foreign Key issue when Updating record for first time
Here is the SQL that is generated and causing the error :
update
Club
set post_code_PostCodeID
= 16392 where ClubID
= 4
Very Simple .. But the actual column name is PostCodeID the post_code is the class/table of the foreignkey . which is added as part of the save process to the column name
the Stupid thing is i had this working , i was in the process of copy the functionality to another table (Person) and got this error , went back to my original table (Club) same error
The editing and the selection/searching of the foreign ID (PostCode)works great
Its just the SQL that is generated ..
Please Help ... major headache from this lol
Thanks
-------------------------
Laravel +10 : filament 3
Table /class with Foreign Key
Schema::create('Club', function (Blueprint $table) {
$table->id('ClubID')->startingValue(1); // need to make this unique over all site !! Central
$table->string('ClubCode',10)->unique()->index();
$table->string('ClubName',50);
$table->string('Address',100)->nullable();
$table->foreignIdFor(PostCode::class,'PostCodeID')->nullable(); *** HERE
$table->string('Email')->unique()->nullable();
$table->string('WebSite',100)->nullable();
});
class Club extends Model
{
use HasFactory;
protected $table = 'Club';
protected $primaryKey = 'ClubID'; // Set the custom primary key
public $incrementing = true; // Set to true for auto-incrementing primary key
public $timestamps = false;
protected $fillable = ['ClubCode','ClubName','Email','Website','Address','PostCodeID'];
Public function PostCode(){
return $this->belongsTo(PostCode::class);
}
}1 Reply
finally got it working by adding the following to the Club Class Public function PostCode(){
return $this->belongsTo(PostCode::class, 'PostCodeID', 'PostCodeID');
}