Filter in Laravel for work not done, incomplete or completed

2

I have been looking for a solution to this problem for some time but I have not found anything that I can adapt.

There are two tables ordensiembras and siembras , one order can have several sowings.

The Ordensiembras model:

    public function sembrado() {
    return $this->hasMany('App\Siembra');

    }

I need to generate a scope with values 1 if it has not been executed ( cantReal==0 ) 2 if it is completed (the sum of cantReal==cantOrden ) and 3 if it is incomplete ( cantReal<cantOrden )

    public function scopeCompletado($query, $fcompletado)
    {
    if($fcompletado==1)
        return $query->where('???????');
    if($fcompletado==2)
        return $query->where('???????');
    if($fcompletado==3)
        return $query->where('???????');
    }

I could review the amount of siembras.canReal in the view and not show the records that do not meet the criteria, but in this case the paging does not work.

    
asked by Diego Zepeda 25.11.2018 в 18:15
source

1 answer

0

What if instead of trying to reuse so much you go on the one hand a little more logical and healthy, create 3 scope.

public function scopeSinEjecutar($query)
{
    return $query->where($this->cantReal, 0);
}

Now, to complete and not complete I would work with the same relationship by placing a conditional, something quite useful to know, staying that way.

public function completado() {
  return $this->hasMany('App\Siembra')->where('catReal', '=', $this->cantOrden);
}


public function scopeCompletado($query)
{
    return $query->has('completado');
}


public function scopeSinCompletar($query)
{
    return $query->has('noCompletado');
}

// With having completed already you will imagine how it is done not completed.

Sure, you can combine that and create a single function, I guess it's valid, but is it really worth it?

    
answered by 27.11.2018 в 04:07