Laravel Get Join Data And Non Join Data

Viewed 36

I am trying to get two tables data using join

    // $participantstation = DB::table('participant_station')
        // ->join('reviews', 'participant_station.id', '=', 'reviews.participant_station_id')
        // ->select('reviews.*', 'participant_station.*')
        // ->where('station_id',$stn->id)
        // ->where('status', '100')
        // ;

Still some partipant are not linked , that means - participant_station.id', '=', 'reviews.participant_station_id

if I try this code it only shows the linked participants

is there any way to show both linked and non-linked participants using such query?

Thank you

2 Answers

Assuming you're trying to fetch all the details using Eloquent ORM here's what you can do:

$participantstation= ParticipantStation::join('participant_station', 'participant_station.id', '=', 'reviews.participant_station')->get(['reviews.*', 'participant_station.*']);

This will join the tables

try this,

Model -> ParticipantStation.php:-

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ParticipantStation extends Model
{
    public function reviews()
    {
        return $this->hasMany('App\Models\Review');
    }
}

Model -> Review.php:-

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Review extends Model
{
    public function participant_station()
    {
        return $this->BelongsTo('App\Models\ParticipantStation','participant_station_id');
    }
}

In controller.php

use App\Models\ParticipantStation;

$participantstation = ParticipantStation::with('reviews')->where('station_id',$stn->id)->where('status', '100')->get();

//dd($participantstation);

Related