lundi 5 juin 2017

Laravel retrieve status from pivot table

i have tables ,Station & Equipment ,a third pivot table station_equipment with additional field "status" besides the respective table ids, the status can be active or inactive, I just want to get the Active elements, but my query keep on retrieving equipments with both status .

the query:

   $eqestacion = Station::where('station.id', $id)
                ->whereHas('equipments', function($query) {

                    $query->where('equipment_station.status','=','active');
                    })->with('equipments')
                      ->get();

Station Model:

public function equipments(){

  return $this->belongsToMany('App\Equipment')->withPivot('status')->withTimestamps();
}

Equiment Model:

 public function stations()

   return $this->belongsToMany('App\Station')
    ->orderBy('pivot_created_at','desc')->withPivot('status')->withTimestamps();
}

thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire