I have a table structure as follows:
- companies table (hasMany shipments, hasMany Packages)
- orders table (belongsTo company, hasMany shipments, hasMany packages)
- shipments table (belongsTo company, belongsTo order, hasMany packages)
- packages table (belongsTo shipment and belongsTo company)
When creating a package entry, I would like to do something like:
$shipment = $order->shipments()->create($request->all());
$package = $shipment->packages()->create();
If I dump-die on the $shipment, it has all the correct ID's in there and I would have expected that the relationships would allow this to create. However creating packages fails on not having a value for order_id (id in orders table)
Am I misunderstanding how eloquent should work here?
EDIT:
Orders
Schema::create('orders', function (Blueprint $table) {
$table->uuid('id');
$table->uuid('company_id');
$table->uuid('order_address_id');
$table->integer('user_id');
$table->string('unique_id');
$table->string('order_ref')->default('');
$table->string('status');
$table->boolean('hold_or_cancel')->default(0);
$table->string('shipping_method');
$table->dateTime('completed_at')->nullable();
$table->timestamps();
$table->unique(array('company_id', 'unique_id'));
Shipments
Schema::create('shipments', function (Blueprint $table) {
$table->uuid('id');
$table->uuid('order_id');
$table->uuid('company_id');
$table->string('status')->default('new');
$table->string('shipment_type');
$table->string('shipping_method')->default('');
$table->string('reference')->default('');
$table->timestamps();
});
package
Schema::create('shipment_packages', function (Blueprint $table) {
$table->uuid('id');
$table->uuid('order_id');
$table->uuid('company_id');
$table->uuid('shipment_id');
$table->string('tracking_number')->default('');
$table->integer('package_weight_g')->nullable();
$table->integer('package_length_cm')->nullable();
$table->integer('package_width_cm')->nullable();
$table->integer('package_height_cm')->nullable();
$table->timestamps();
});
To create an order I use the following which works fine.
$address = OrderAddress::create($data);
$order = $address->order()->create($data);
To create a shipment and package I am trying to use:
DB::transaction(function () use ($order, $request) {
$shipment = $order->shipments()->create($request->all());
$package = $shipment->shipmentPackages()->create();
});
However I am not able to pull through the company or order id using this method, which I would like to do in order to easily see lists of shipments and packages which belong to orders or companies i.e. a shipment belongsTo an order AND a company. For the shipment package, this belongs to a shipment, an order and a company.
On reflection, maybe I am trying to relate each table to too many things. My reasoning behind this was "what if I want to find out all the packages which a company has shipped, therefore I should include the company_id in the packages table". However, maybe its best to just see which shipments are owned by the company and work the packages out from there.
packages table (belongsTo shipment and belongsTo company)
but has a FK to orders? I am a little confused what is happening right here. Wouldn't that FK imply belongsTo Order not Shipment? It seems like you want$shipment->order()->packages()->create()
or something similar. – Alex Harris