How to use where with relationships.
Created 6 years ago by gonziis

Hey, Im trying to fetch all data in my controller for my model which has multiple relationships

public function index(LeemumiRepositoryInterface $leemumi)
{
       return $this->view->make("xxxxx::leemumi/index");
}

dd($leemumi) returns

LeemumiRepository {#2125 ▼
  #model: LeemumiModel {#2126 ▼
    #searchable: true
    #table: "leemumi_leemumi"
    #titleName: "name"
    #rules: array:21 [▶]
    #fields: array:21 [▶]
    #dates: array:4 [▶]
    #relationships: array:7 [▼
      0 => "file"
      1 => "files"
      2 => "lemumu_categories"
      3 => "lietu_classification"
      4 => "likumu_article"
      5 => "nannce"
      6 => "adressat"
    ]

As you can see the relationship with nannce is there, but how to write code to get specific Leemumi that has nannce with specific ids? I imagine it would be something like $leemumi->where("nannce.id","=",10) for example

Please help, Im really struggling with this, some hint at least would be nice.

ryanthompson  —  6 years ago

Relationship fields always add a Laravel relationship method to the model. So the PHP API would look like this for example:

$entry->relationFieldSlug()->where('foo', 'bar')->first();

Twig would be the same way:

{% set first = entry.relationFieldSlug().where('foo', 'bar').first() %}