In my Laravel app I was doing an ordinary query on a model (ModelA
) whilst making use of SQL_CALC_FOUND_ROWS
and then performing SELECT FOUND_ROWS()
afterwards to retrieve the count of all records since the first query used LIMIT
and OFFSET
.
This worked perfectly fine, but now that I've added a relationship to the model I was querying above, if I do the same query but using with->('modelB')
, this query is performed after the initial query and before SELECT FOUND_ROWS()
so I get the count of the ModelB
results instead of ModelA
as I was expecting.
Is there a way to make this work as desired where I get the count of the first query and not the relationship?
e.g. This works fine:
$query = ModelA::select([DB::raw("SQL_CALC_FOUND_ROWS *")])
->where('active', 1);
// conditional ->where()'s
$query->skip(intval($skip))
->take(intval($take))
->orderBy($column, $dir);
$results = $query->get();
$total = DB::select(DB::raw("SELECT FOUND_ROWS() AS 'total';"))[0]->total;
but changing the first line to this doesn't:
$query = ModelA::with('modelB')
->select([DB::raw("SQL_CALC_FOUND_ROWS *")])
->where('active', 1);
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire