Rodrigo Cabrera
Rodrigo Cabrera

Reputation: 164

Laravel pass variable from controller query to model function

I have a function on my model that check active status in equipment_station table,

originally it only checked one status, now i need to pass a parameter to the model with active or inactive values, I've tried this with no success.

controller: controller query

model : model image

How can i change the parameter in the model, I send status from controller as inactive or active.

thanks in advance

Upvotes: 1

Views: 1524

Answers (2)

linktoahref
linktoahref

Reputation: 7972

You could Constraint Eager Loading by specifying additional query constraints for the eager loading

$historial_estaction = 
Estacion::where('estacion.id', $id)
   ->whereHas('equipos', function($query) use ($estado1) {
        $query->where('equipo_estacion.estado', $estado1);
    })->with(['equipos' => function ($query) use ($estado1) {
        $query->where('estado', $estado1);
    }])->get();

Model:

public function equipos()
{
    return $this->belongsToMany('App\Equipo')
                ->withPivot('estado')
                ->withTimestamps();
}

Upvotes: 1

cwang
cwang

Reputation: 1104

You could you query scope

public function scopeActive($query, $active)
{
   return $query->where('active', $active);
}

then you can call it in your model

Estacion::active(true)->get();

Upvotes: 1

Related Questions