1
votes

Is it possible to create custom relationships in Laravel like this,

SELECT tasks.* 
FROM tasks
LEFT JOIN priorities ON tasks.priority_identifier LIKE CONCAT('%', priorities.identifier)
WHERE priority.id = 10

I could use query builder to create this join but how can i return this as a laravel relationship ?

Edit

I'm trying to do this without using existing relationship methods(hasMany, hasOne etc)

2
Custom Laravel Relations? I think this link might help youAnuj Shrestha

2 Answers

0
votes

Yes, you can add conditions to Laravel relations just like any other query builder. I don't know what your relation will look like, but you could do this for example:

public function tasks()
{
    return $this->hasMany(Task::class)
        ->where('priority', 10);
}
0
votes

I don't think you can create a custom relationship, unless overriding Eloquent base class.

And add your own.

But you can use Eloquent scopes alternative to custom relationships.

Also take a look here and here