mardi 22 septembre 2015

how to construct a function to detect if a relation is active or expired - eloquent

I have two models: Entity and Entityrelation. The Entities can enter relationships. I try to construct a table with lists of present and past relations

in the Entity I have this function:

// Entity.php
public function activeMembership()
{
    return $this->hasOne('App\Models\Entityrelation', 'entitychild_id')->NotCeased()->Where('relationtype_id', '=', '4');
}

The NotCeased is a scope defined in the Entityrelation model:

// Entityrelation.php
public function scopeCeased($query)
{
    return $query->where('ceased', 1);
}
public function scopeNotCeased($query)
{
    return $query->where('ceased', 0);
}

The ceased is a column in the relationships table.

So when I want to get a number of active memberships, I just do this :

$object = Entity::find(1);

and in my view

$object->public function activeMembership()->count();

and a simple @foreach loop produces me a list of all active memberships of the Entity with id = 1.

My question

I want to design a function, which check if a given Entity was a child in an relation with an organization with ID = $parent_id (in entityparent_id).

  • returns 1 when an active relationship was found.
  • returns 0when the found relation was expired (ceased = 1)
  • returns null when no relation with the organization ever existed


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire