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

thestapler's avatar

Working with Pivots

OK, I posted something before about my dilemma, but now, I have run into a problem.

I have 3 tables:

vendor (actual vendor)
vendor_client (client specific vendor data)
vendor_vendor_client (pivot - vendor_id - vendor_client_id -- has client_id as a field))

Now, I have the associations done with the model, and I can get the details, etc etc... but now, how can I search though it?

$vendor->where('vendor_vendor_client.client_id', 2)->get()

doesn't get me what I want...

$vendor->details()->where('vendor_vendor_client.client_id', 2)->get();

that gets me a collection, but doesn't get me any items, since the vendor_id is null.

$vendor->find(1)->details()->where('vendor_vendor_client.client_id', 2)->get();

gets me what I want... sorta... because I want to search for a vendor by 'like name'

$vendor->where('name', 'like', 'v%')->details()->where('vendor_vendor_client.client_id', 2)->get();

that just fails...

Is there a way to let the model know that the pivot exists, and to just query that? am I just going about it the wrong way?

HELP!?

0 likes
3 replies
thestapler's avatar

Vendor.php

<?php namespace App\Jade\Vendors;

use Illuminate\Database\Eloquent\Model;

class Vendor extends Model
{
    protected $table = 'vendor';
    protected $fillable = ['name'];

    public function details()
    {
        return $this->belongsToMany('App\Jade\Vendors\VendorClient', 'vendor_vendor_client', 'vendor_id', 'vendor_client_id')->withPivot('client_id');
    }
}

VendorClient.php

<?php namespace App\Jade\Vendors;

use Illuminate\Database\Eloquent\Model;

class VendorClient extends Model
{
    protected $table = 'vendor_client';

    public function vendor()
    {
        return $this->belongsTo('\App\Jade\Vendors\Vendor', 'vendor_vendor_client.vendor_client_id', 'id');
    }
}

VendorVendorClient.php

<?php namespace App\Jade\Vendors;

use Illuminate\Database\Eloquent\Model;

class VendorVendorClient extends Model
{
    protected $table = 'vendor_vendor_client';

    public function vendor()
    {
        return $this->belongsTo('\App\Jade\Vendors\Vendor', 'vendor_id', 'id');
    }

    public function vendorClient()
    {
        return $this->belongsTo('\App\Jade\Vendors\VendorClient', 'vendor_client_id', 'id');
    }

    public function client()
    {
        return $this->belongsTo('App\Jade\Clients\Client', 'client_id', 'id');
    }
}
thestapler's avatar

Now, once this is done, I have to do the similar thing with Parts/items... since each vendor has their own part number, and each client may have their own number reference, as well as their price, and contract numbers... etc etc... but I thought I would start with trying to figure out my vendor.

If my tables should be changed, or anything else... I am not opposed to doing that, as I am just staring out rebuiling my OLD code.

Thanks!

Please or to participate in this conversation.