0
votes

I have a table structure as follows:

  1. companies table (hasMany shipments, hasMany Packages)
  2. orders table (belongsTo company, hasMany shipments, hasMany packages)
  3. shipments table (belongsTo company, belongsTo order, hasMany packages)
  4. 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.

1
When creating package, did you provide the company_id? as package belongsTo company.Abid Raza
Why do not you create it manually? $package = Package::create([ 'order_id' => $shipment->order_id, ]);Eleazar Resendez
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
The package belongs to the shipment and the shipment belongs to the order. They each have the inverse has many too. I was hoping that after creating the shipment (which contains the company and order is) I could then create the package without having to re-specify these ids again. Currently only the shipment id is pulled through to the package using the relationship. Is it possible to pull through all 3 id's without manually adding them to create?Adam Lambert

1 Answers

0
votes

This looks like your relationship is wrong, order_id should be in the shipments table so it related to id on order.