hasManyThrough using a pivot on the second model.

Posted 2 months ago by DPlachkov

Hello guys i have 3 classes

class Expedition extends Model
{
    public function bags()
    {
        return $this->hasMany(Bag::class);
    }

    public function shipments()
    {
        return $this->hasManyThrough(Shipment::class, Bag::class);
    }
}
class Bag extends Model
{
public function expedition()
    {
        $this->belongsTo(Expedition::class);
    }

    public function shipments()
    {
        return $this->belongsToMany(Shipment::class, 'linked_bags_shipments', 'bag_id', 'shipment_id');
    }
}

And the Shipments class.

What I'm trying to do is call the $expedition->shipments() method, but it doesnt work, since its trying to get it through joining the bags and shipments table, at that model I am using a pivot table called "linked_bags_shipments" . Is there a way to use the hasManyThrough with a pivot table between the 2 models

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.