Those are the queries that form the collection to be pushed to the laravel datatables builder:
foreach (Session::get('trienios') as $trienio) {
$oeTrienios = $oeTrienios->where('data_trienio', $trienio->trienio)->whereHas('curso', function ($query) use ($trienio) {
$query->where('curso', $trienio->curso);
});
}
$union = Trienio::with('curso')->whereHas('curso', function ($query) use ($coordinatedCourse) {
$query->where('curso', $coordinatedCourse);
})->union($oeTrienios);
$trienios = \DB::table(\DB::raw("({$union->toSql()}) as x"))->select(['data_trienio']);
There is a tutorial on the official laravel-datatables site that "explains" how to work with united queries, however it is pretty vague and can't realistically explain anything, and besides, when I tried to add a code they had on the tutorial:
$trienios = \DB::table(\DB::raw("({$union->toSql()}) as x"))
It gives me the following error:
SQLSTATE[HY000]: General error: 2031 (SQL: select count(*) as aggregate from (select `data_trienio` from ((select * from `trienios` where exists (select * from `cursos` where `trienios`.`curso_id` = `cursos`.`id` and `curso` = ?)) union (select * from `trienios` where `data_trienio` = ? and exists (select * from `cursos` where `trienios`.`curso_id` = `cursos`.`id` and `curso` = ?) and `data_trienio` = ? and exists (select * from `cursos` where `trienios`.`curso_id` = `cursos`.`id` and `curso` = ?))) as x) count_row_table)
If I, however, attach the parameter ->get() to the ->union($oeTrienios), it will work just fine, however, the collection will come unorderable on the datatable.
How do I solve this issue? Any help would be very welcome.
P.S - Link to the demo: https://datatables.yajrabox.com/fluent/union