Love verma
Love verma

Reputation: 66

Custom foreign key & local key in hasMany relation : Laravel

I want to use hasMany relation with raw sql foreign key and local key. I have to relate them on the basis of date that's why I have to use raw sql functions as foreign key and local key.

I've tried to customize the hasMany relation but no success.

public function auctionVehicles()
{
    return $this->customHasMany(
        'App\Models\ManheimVehicle',
        \DB::raw("SUBSTRING(sale_date, 1, 10)"),
        \DB::raw("DATE_FORMAT(
             STR_TO_DATE(auction_date,'%b %d, %Y'), '%d/%m/%Y')")
        )
    );
}

Is this possible?

Upvotes: 1

Views: 3118

Answers (1)

Ali Jawadi
Ali Jawadi

Reputation: 131

I can't understand why are you using this customHasMany with a raw query and what's your issue exactly, but I guess you're looking for this:

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

You can use the second and third argument to override the default foreign key and local key on relationships.

Upvotes: -1

Related Questions