jeudi 22 décembre 2016

Laravel Eloquent relation overwrites column?

A User can create Auctions and Auctions can have an appending Purchase.

I want to fetch all Purchases ( the one that HE SOLD - this user created an Auction and someone else purchased it) from one User by referencing via the Auctions table:

Auctions id | user_id

Purchases id | user_id | auction_id

My Auctions table has a column user_id, which has the same name as the column user_id from the table Purchases.

I thought I can fetch all Purchases with the hasManyThrough method like this:

User Model:

  public function sold_purchases()
  {
    return $this->hasManyThrough(
      'App\Purchase', 'App\Auction'
      );
  }

It works nearly as expected BUT:

The field user_id from the fetched results always has the user_id from the Auction it is referenced by and not the correct user_id as it is in the databse. As they have the same name I guess somehow this gets messed up.

Is there a way to give a synonym to one of those columns? Or am I doing something else wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire