Friday, May 11, 2018

Laravel Eloquent HasMany / HasOne Additional On Clauses - development

Laravel Eloquent HasMany / HasOne Additional On Clauses

Good morning,

Apologies if this has been ask before but we are unable to find any answer to an issue we are having.

We are working with a legacy database that is not owned by us (read-only) and are attempting to use Eloquent (Models) in Laravel to solve some common issues.

Is it possible to setup Eloquent’s Eager-loading to create additional ON clauses to the HasMany / HasOne relationship builder?

Please see below of what we are trying to achieve without raw queries.

public function policy()
{
    return $this->hasMany(Policy::class, 'Group', 'Group')
        // This breaks as `on` isn't defined on EloquentBuilder. Is this concept possible? Multiple on clauses
        ->on('Reference', 'Reference');
}

In our controller we have attempted the following which also fails.

 Vehicle::with([
        'policy' => function ($query) {
            // Model isn't instantiated yet, but we need an additional on clause here
            $query->on('Reference', 'Reference');
        }
   ]);

Can the above be achieved or do we have to revert back to using raw queries?

Thank you in advance for any help.



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-eloquent-hasmany-hasone-additional-on-clauses/
via Lzo Media

No comments:

Post a Comment