'Laravel save eloquent model with relations
Sorry if this is something really simple but I haven't been able to find an answer after hours of searching...
I have this object with relations:
$this->versionSelected = new StyleVersions();
$this->versionSelected = StyleVersions::with([
'colourways.colourway_yarns.yarns.spinners',
])->find($id);
Which is edited using wire:model:
wire:model="versionSelected.name"
and then verified:
protected $rules = [
'versionSelected.name' => 'required',
'versionSelected.factories_id' => 'required',
'versionSelected.yarn_transportation_cost' => 'required',
'versionSelected.yarn_transportation_currency' => 'required',
'versionSelected.accessories_cost' => 'required',
'versionSelected.accessories_currency' => 'required',
'versionSelected.transport_cost' => 'required',
'versionSelected.gmt_transportation_currency' => 'required',
'versionSelected.cmt' => 'required',
'versionSelected.gmt_weight' => 'required',
'versionSelected.area_office_commission' => 'required',
'versionSelected.notes' => 'required',
'versionSelected.style_version_prices' => 'required',
];
I then want to save that object to the database, but it is having issues with the relations.
$this->versionSelected->save();
When this is called I get the followin error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'style_version_prices' in 'field list' (SQL: update
style_versionssetstyle_version_prices= [{"id":10,"style_versions_id":4,"price":54.7,"notes":"Quo dolorum ab dolores.","created_at":"2022-02-25T10:19:23.000000Z","updated_at":"2022-02-25T10:19:23.000000Z"},{"id":18,"style_versions_id":4,"price":75,"notes":"Voluptatibus aut nihil.","created_at":"2022-02-25T10:19:23.000000Z","updated_at":"2022-02-25T10:19:23.000000Z"},
Is there a way I can update without it looking at the relations, which I can update separately? Thanks in advance! =D
EDIT:
Here is the code for the style_versions table:
Schema::create('style_versions', function (Blueprint $table) {
$table->id();
$table->foreignId('styles_id')->constrained()->onDelete('cascade');
$table->foreignId('factories_id')->constrained()->onDelete('cascade');
$table->text('name')->nullable();
$table->float('yarn_transportation_cost');
$table->float('gmt_weight');
$table->float('cmt');
$table->float('accessories_cost');
$table->float('transport_cost');
$table->text('notes')->nullable();
$table->float('area_office_commission')->default(0);
$table->float('yarn_cost');
$table->text('cmt_currency')->nullable();
$table->text('accessories_currency')->nullable();
$table->text('yarn_transportation_currency')->nullable();
$table->text('gmt_transportation_currency')->nullable();
$table->timestamps();
});
And the model:
class StyleVersions extends Model
{ use HasFactory;
protected $fillable = [
'styles_id',
'factories_id',
'yarn_transportation_cost',
'gmt_weight',
'cmt',
'accessories_cost',
'transport_cost',
'area_office_commission',
'notes',
'cmt_currency',
'accessories_currency',
'yarn_transportation_currency',
'gmt_transportation_currency',
'yarn_cost',
];
public function styles()
{
return $this->belongsTo(Styles::class);
}
public function factories()
{
return $this->belongsTo(Factories::class);
}
public function colourways()
{
return $this->hasMany(Colourways::class);
}
public function style_version_prices()
{
return $this->hasMany(StyleVersionPrices::class);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
