I need to query Eloquent with union(), but using Model's method on a with().
How do I select these fields correctly?
$queryBuilder1 = OneModel::select('column1', 'column2', 'columnForRelationTwoModel')
->select('pkForRelationNextWith')
->with(['relationTwoModelInOneModel' => function($builder]{
$builder->select('column3')
->select('pkForRelationNextWith')
->with(
['relationTreeModelInTwoModel' => function($builder){
$builder
->select('pkForRelationNextWith')
->with(['relationFourModelInTreeModel' => function($builder) {
$builder->select('column4');
}]);
}
]
);
});
$queryBuilder2 = ModelX::select(
'column1_X',
DB::raw("'A' as column2"),
'column3_X',
'column4_x'
)
->groupBy('column1_X');
$queryUnion = $queryBuilder1->union($queryBuilder2)->orderBy('column1')->orderBy('column3')->get();
My Result:
SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns [...]
I expect the output of the 4 united parameters.