I have a Laravel project with some models, relationships and dynamic scopes into models to perform some queries along model relationships. I want to query these models using a GraphQL endpoint using the Lighthouse package. Is there any way to call those dynamic scopes from the GraphQL schema?
I've tried the directive @where(clause: "...") with no success.
This is the scope at the code of my model:
class Solicitud extends Model {
...
public function movimientos()
{
return $this->hasMany('App\Movimiento', 'id', 'id_solicitud_movimiento');
}
public function scopeConEstado($query, $id_estado)
{
$query->whereHas('movimientos', function($q) use ($id_estado) {
$q->where('id_solicitud_estado', '=', $id_estado);
})->get();
}
}
When I run the GraphQL query I get a SQL error that says:
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: la sintaxis de entrada no es válida para integer: «id_estado» (SQL: select * from \"solicitud\" where exists (select * from \"solicitud_movimientos\" where \"solicitud\".\"id_solicitud_movimiento\" = \"solicitud_movimientos\".\"id\" and \"id_solicitud_estado\" = id_estado))
However, if I change $id_estado by a simple 1 (for example) in the code, I get the results as expected for any project who has items with id_solicitud_estado = 1, with no error.
When I launch with tinker the query calling the scope, it works.
Solicitud::conEstado(1)->get();
What am I doing wrong? What it happens with the @where(clause:) directive from Lighthouse? Maybe another directive should be used?
Thanks.