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

thestapler's avatar

Eloquent DB questions....

Ok... say I have 3 tables. client vendor vendor_details

The vendor is specific vendor data... and the vendor_details contains vendor data specific for the client.

Vendor Model:

<?php namespace App\Vendors;

use Illuminate\Database\Eloquent\Model;

class Vendor extends Model
{

    protected $table = 'vendor';

    public function details()
    {
        return $this->hasMany('App\Vendors\VendorDetails', 'vendor_id', 'id');
    }
}

Vendor Details Model:

<?php namespace App\Vendors;

use Illuminate\Database\Eloquent\Model;

class VendorDetails extends Model
{

    protected $table = 'vendor_details';

    public function __construct()
    {
    }

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

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

My question is, is how do I get specific data from the vendor detail, that is for the client 1 (say that is set in a session variable)?

client
[
    id=1
    name=Foo
]
client
[
    id=2
    name=Bar
]
vendor
[
    id = 1
    name = Ven1
]
vendor_details
[
    id=1
    vendor_id=1
    client_id=1
    vendor_code=11111
]
vendor_details
[
    id=2
    vendor_id=1
    client_id=2
    vendor_code=22222
]

Sorry, I hope my question makes sense...

0 likes
4 replies
Mehdi_Souihed's avatar

If what you mean is how to resolve your foreign keys and get joined data from your three tables, you could do the following :

VendorDetails::with('vendor','client')->where('client_id', =, Session::get('client_id'))->get() ;

This method is called Eager-Loading

JarekTkaczyk's avatar

@thestapler What you have here is a many-to-many relation between Client and Vendor, so the best you can do would be this:

// Client model
public function vendors()
{
   return $this->belongsToMany('Vendor', 'vendor_details') // adjust namespace for Vendor
     ->withPivot('vendor_code'); // add more fields if required
}

// then
$vendor = $client->vendors->find($vendorId);
if ($vendor) $vendor->pivot; // if vendor is found, ie. not null, then pivot will hold vendor_details data
$vendor->pivot->vendor_code; // the field you want
thestapler's avatar

Ok, I am just building an app that I built approx 8 years ago, it is time for an upgrade :)

Basically, the structure is:

client              - holds all client data
vendor          - holds all vendor data
vendor_client       - holds information about the vendor, specific to the client (since each client can have different information on each vendor)
item                - holds item information for each vendor
item_client     - holds specific item information for each client

Is there a better way to do this? I know how I would do it with my old way of coding, but I need to get away from the way I used to code, and do more gooder coding :P

So, since I am just redeveloping it, I can do it pretty much any way I need to. But I need to understand why the way it is being done, is the way to do it.

Thanks for any help you can offer!

thestapler's avatar

Some of the logic:

When I am logged in as the client: I should be able to see all the vendors that are attached to my client id. I should be able to see all the items and vendors that are attached to my client id.

When I am the vendor: I should be able to see all the clients that have me listed. I should also be able to see all my items, as well as any vendors they are attached to and what they call them as well (if they are different).

I am not asking anyone to write the app for me, but to give me some advice on how to go about doing this with 'proper' coding, and database/laravel coding practices.

As I said, I am new to laravel, and I know how I would have done this in my old ways, but I am trying to change! :)

Please or to participate in this conversation.