mmasomi73's avatar

Problem in Use Union With Pagination in Eloquent

Hi, I Want To Union two Model And Paginate Result But Get Error, In Bellow See My Call Code And Result:

    $this->archive = $this->archive
            ->select (['id','property_id','plan_id','pay_id','price','period','start_at','expire_at'])
            ->whereHas('property',function ($query) use ($user){
            $query->select(['user_id','type_id','title'])->where('user_id',$user);
            })->with(['property'=>function($query){
                $query->select('title','id');
            },'property.owner'=>function($query){
                $query->select('name','family','id');
            },'plan'=>function($query){
                $query->select('title','price','id','status');
            },'transaction'=>function($query){
                $query->select('port','price','id','status','payment_date');
            }]);
        $this->model = $this->model
            ->select (['id', 'property_id','plan_id','pay_id','price','period','start_at','expire_at'])
            ->whereHas('property',function ($query) use ($user){
            $query->select(['user_id','type_id','title'])->where('user_id',$user);
            })->with(['property'=>function($query){
                $query->select('title','id');
            },'property.owner'=>function($query){
                $query->select('name','family','id');
            },'plan'=>function($query){
                $query->select('title','price','id','status');
            },'transaction'=>function($query){
                $query->select('port','price','id','status','payment_date');
            }])
            ->union($this->archive)->orderBy('expire_at','DESC')->paginate ($paginate);

And Result Of Them Is:

    SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns

Please Help Me

0 likes
2 replies
Sergiu17's avatar
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order

Please or to participate in this conversation.