Eloquent many-to-many returning an empty value

  database, eloquent, join, laravel, php

Good day. I am trying to convert some of my models to use Eloquent mechanisms from table joins. I have three tables, and I want to use one of them as a pivot table. The three tables are shown below
First table (cp_cases_counsel)
cp_cases_counsel

Followed by (cp_counsel)
cp_counsel

Then,(cases_sc)
cases_sc

I am trying to implement a many-to-many relationship between cases_sc and cp_counsel using cp_cases_counsel as the pivot table.

These are my models

use AppModelsSupremeCases;
use IlluminateDatabaseEloquentModel;
use AppModelsCounselCase;

class Counsel extends Model
{
    //
    protected $fillable = [];
    protected $table = 'cp_counsel';
    protected $connection = 'mysql2';


    public function supreme()
    {
        return $this->belongsToMany(SupremeCases::class,'cp_cases_counsel','counsel_id','suitno');
    }

}

For the Cases Model

use IlluminateDatabaseEloquentModel;
use AppModelsCounsel;

class SupremeCases extends Model
{
    //
    protected $connection = "mysql2";
    protected $fillable = [];
    protected $table = 'cases_sc';
    public $timestamps = false;

    public function counsels()
    {
        
        return $this->belongsToMany(Counsel::class,'cp_cases_counsel','counsel_id','suitno');
    }
}

Getting the counsels and their cases using
$counsels = Counsel::with('supreme')->get();

I get an empty result for the supreme node in the response as shown below
enter image description here

Please, what I’m I doing wrong?

Source: Laravel

Leave a Reply