jeudi 18 octobre 2018

with relation retrieve all the records- Laravel

I want to retrieve only the active organisations but the the database return all the records.Here is my code.you can see below a record should not be returned as a listed organisation I include below the code and my Model and The result I got from the database

      $organisations = Organisation::with(['year_status' => function ($query) use ($year_id) {
                    $query->where(['year_id' => $year_id, 'is_listed' => 1]);
                }])->get();



public function year_status()
    {
        return $this->hasMany('App\OrganisationYearStatus');
    } 

 and what I got :

11 => Organisation {#630 ▼
      #fillable: array:8 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:14 [▼
        "id" => 39
        "organisation" => "Test Organisation"
        "sector_id" => 1
        "country_id" => 1
         ]
      #original: array:14 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "year_status" => Collection {#632 ▼
          #items: []
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire