'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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1