Home > Net >  How to call relationship in union tables laravel
How to call relationship in union tables laravel

Time:05-10

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.

CodePudding user response:

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();

CodePudding user response:

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);

CodePudding user response:

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();
  • Related