'How to call relationship in union tables laravel

I have two tables and made union for these two tables code here below

 $posts2 = DB::table('applicants')
            ->select('applicants.id', 'surname', 'name', 'middle', 'order', 'position', 'updated_at')
            ->where('appuserstatus_id', 1)
            ->orWhere('appuserstatus_id', 14);
        $posts = DB::table('applications')
             ->select('applications.id', 'surname', 'name', 'middle', 'order', 'position', 'updated_at')
             ->where('appuserstatus_id', 1)
             ->orWhere('appuserstatus_id', 14)
             ->union($posts2)
             ->orderBy('updated_at', 'DESC')
            ->get();

Also, I have similar relationships in these two models Model Applicant

public function appuserstatus() {
        return $this->belongsTo(Appuserstatus::class, 'appuserstatus_id', 'id');
    }

Model Application

 public function appuserstatus() {
        return $this->belongsTo(Appuserstatus::class, 'appuserstatus_id', 'id');
    }

But I cannot call their relationships in blade like this it doesnt work

@foreach($posts as $post)
<div>{{ $post->name }}</div>
<div>{{ $post->appuserstatus->name }}</div>
@endforeach

It works if I use eloquent model like

Applicant::with('appuserstatus')->get();

Please help me, I am new in Laravel framework.



Solution 1:[1]

You can try this

Applicant::with('appuserstatus')
    ->fromSub(function ($query) {
        $query->from('applications')
            ->select('applicants.id', 'surname', 'name', 'middle', 'order', 'position', 'updated_at')
            ->where('appuserstatus_id', 1)
            ->orWhere('appuserstatus_id', 14)
            ->union(DB::table('applicants')
                ->select('applicants.id', 'surname', 'name', 'middle', 'order', 'position', 'updated_at')
                ->where('appuserstatus_id', 1)
                ->orWhere('appuserstatus_id', 14)
            )
            ->orderBy('updated_at', 'DESC');
    },'applicants')
    ->get();

Solution 2:[2]

you can write two Separately eloquent query and then merge them :

$applicant = Applicant::with('appuserstatus')->get();
$applications = Applications::with('appuserstatus')->get();
$result = $applicant->merge($applications);

Solution 3:[3]

Thank you guys for your replies. But I decided to use joins in my query like this

$posts2 = DB::table('applicants')
            ->join('appuserstatuses', 'applicants.appuserstatus_id', '=', 'appuserstatuses.id')
            ->select('applicants.id', 'applicants.surname', 'applicants.name', 'applicants.middle', 'applicants.order', 'applicants.position', 'applicants.updated_at', 'appuserstatuses.name as subjectName')
            ->where('appuserstatus_id', 1)
            ->orWhere('appuserstatus_id', 14);
        $posts = DB::table('applications')
             ->join('appuserstatuses', 'applications.appuserstatus_id', '=', 'appuserstatuses.id')
             ->select('applications.id', 'applications.surname', 'applications.name', 'applications.middle', 'applications.order', 'applications.position', 'applications.updated_at', 'appuserstatuses.name as subjectName')
             ->where('appuserstatus_id', 1)
             ->orWhere('appuserstatus_id', 14)
             ->union($posts2)
             ->orderBy('updated_at', 'DESC')
            ->get();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samir Rustamov
Solution 2 amirhosein hadi
Solution 3 ???????? ?????????