0

I am trying to create a relationship between permission and item_permission. The problem is that I need to check if the two columns on each table match, permission.op_id = item_permission.op_id and permission.screen_id = item_permission.screen_id.

class Permission extends Model
{
    public function itemPermission()
    {
        // This should also match the screen_id column in each table.
        return $this->hasMany('App\ItemPermission', 'op_id', 'op_id');
    }
}

Using SQL is easy; I use "and" in the inner join between permission and item_permission. But with Eloquent, I cannot achieve it. I've tried all day to solve this problem but I couldn't. I want to be able to use a query like the following.

$permission->with('itemPermission)->get()
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
diego alves
  • 23
  • 1
  • 5

1 Answers1

0

I'm not sure what your top level relation is, but you are probably looking for has, or for more filter control, wherehas. Generally this will help to select the secondary hasMany relationships where they exist.

The Laravel manual is pretty helpful here, but for a great explanation of the eloquent concepts in general, see this StackOverflow answer.

Watercayman
  • 7,970
  • 10
  • 31
  • 49
  • @diego, if this answer helped you to get to your next question on how to use `has`, please mark it as accepted. – Watercayman Jul 23 '19 at 18:21