Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajsmith_codes's avatar

Eloquent relationship works with seeder, but not with form entry.

I have a many-to-many relationship between Purchase Orders and Shipments.

Shipments are from Vendors through Purchase Orders.

When I use a seeder to set up a few dummy shipments, the eloquent relationship brings up the Vendor's name in the view. If I enter the shipment with a form, it doesn't bring up the name. However, the records exist in the database.

What am I missing?

Tables:

Purchases - has vendor_id 

Shipments - dates and tracking info

Purchase_Shipment - purchase_id and shipment_id

Relationships

Vendor:

    public function purchases()
    {

        return $this->hasMany('App\Models\Purchasing\Purchase');

    }

    public function shipments()
    {
        return $this->hasManyThrough('App\Models\Purchasing\Shipment', 'App\Models\Purchasing\Purchase',
            'purchases.id', 'purchases.vendor_id');
    }

Purchase:

    public function shipments()
    {

        return $this->belongsToMany('App\Models\Purchasing\Shipment', 'purchase_shipment');

    }


    public function vendor()
    {

        return $this->belongsTo('App\Models\Vendor\Vendor');

    }

Shipment:

    public function purchase(){

        return $this->belongsToMany('App\Models\Purchasing\Purchase', 'purchase_shipment');

    }

    public function vendor(){
        return $this->hasOneThrough('App\Models\Vendor\Vendor', 'App\Models\Purchasing\Purchase',
            'purchases.vendor_id', 'purchases.id');
    }

Here are the seeders:

Purchases

        DB::table('purchases')->insert([
            ['number' => 1231, 'vendor_id' => 1, 'status' => 'In Progress', 'active' => 1,
                'date_po_sent' => null, 'date_po_confirmed' => null, 'slug' => '1231', 'all_items_received' => 0],

            ['number' => 1232, 'vendor_id' => 2, 'status' => 'Received', 'active' => 0,
                'date_po_sent' => today()->subDays(6), 'date_po_confirmed' => today(), 'slug' => '1232', 'all_items_received' => 0],

            ['number' => 1233, 'vendor_id' => 3, 'status' => 'Cancelled', 'active' => 1,
                'date_po_sent' => today()->subDays(10), 'date_po_confirmed' => today()->addWeekdays(1), 'slug' => '1233', 'all_items_received' => 0],

            ['number' => 1234, 'vendor_id' => 16, 'status' => 'In Progress', 'active' => 1,
                'date_po_sent' => today()->subDays(6), 'date_po_confirmed' => today()->subDays(5), 'slug' => '1234', 'all_items_received' => 1],

            ['number' => 1235, 'vendor_id' => 50, 'status' => 'In Progress', 'active' => 1,
                'date_po_sent' => today()->subDays(2), 'date_po_confirmed' => today()->subDays(1), 'slug' => '1235',  'all_items_received' => 1],

            ['number' => 1236, 'vendor_id' => 65, 'status' => 'In Progress', 'active' => 1,
                'date_po_sent' => today()->subDays(16), 'date_po_confirmed' => today()->subDays(11), 'slug' => '1236',  'all_items_received' => 0],

            ['number' => 1237, 'vendor_id' => 76, 'status' => 'In Progress', 'active' => 1,
                'date_po_sent' => today()->subDays(1), 'date_po_confirmed' => today()->subDays(1), 'slug' => '1237',  'all_items_received' => 1],
        ]);

Shipments:

        DB::table('shipments')->insert([
            ['id' => 1,'requested_ship_date' => today()->addDays(4), 'expected_delivery_date' => today()->addWeekdays(5),
                'actual_ship_date' => null, 'received_date' => null],
            ['id' => 2,'requested_ship_date' => today()->addDays(22), 'expected_delivery_date' => today()->addWeekdays(26),
                'actual_ship_date' => null, 'received_date' => null],
            ['id' => 3,'requested_ship_date' => today()->addDays(13), 'expected_delivery_date' => today()->addWeekdays(15),
                'actual_ship_date' => null, 'received_date' => null],
        ]);

purchase_shipment:

        DB::table('purchase_shipment')->insert([
            ['purchase_id' => 1, 'shipment_id' => 1],
            ['purchase_id' => 1, 'shipment_id' => 2],
            ['purchase_id' => 2, 'shipment_id' => 3],
        ]);
0 likes
5 replies
Romain's avatar

Hey,

a few questions:

  • What do you mean by "brings up"?
  • Can you show how you actually retrieve the data in both cases, if that's the issue?
Romain's avatar

Yeah sure we can try that. Maybe simply a Github repo would be enough

ajsmith_codes's avatar

In the view, I can use

{{ $purchaseOrder->vendor->name }}

and the vendor name shows up.

Romain's avatar

In that case, maybe the form is the issue as well as the controller. Are you sure that the data is saved properly in the database? With the proper IDs being linked?

Please or to participate in this conversation.