1

I'm using Laravel and Eloquent. I have a courses table and a Pivot table that holds the related courses.

My pivot table named relatedCourses has 2 columns, course1 & course2. enter image description here

In the above example, Course ID 5 is related with Course ID 7.

$data["course"] = course::where("isActive",1)->with('related')->find(5);

This works and brings ID 7 as related.

If i try $data["course"] = course::where("isActive",1)->with('related')->find(7) it should bring ID 5 as related, this is what i'm trying to achieve.

My code is like this in the model :

class course extends Model
    {
        public function category()
        {
            return $this->hasOne('App\courseCategory',"categoryId");
        }

        public function related()
        {
            return $this->belongsToMany('App\Models\course', 'relatedCourses', 'course1', 'course2');
        }
    }
Simos Fasouliotis
  • 1,383
  • 2
  • 16
  • 35

1 Answers1

0

You can define two relationships for this query.

class course extends Model
    {
        public function category()
        {
            return $this->hasOne('App\courseCategory',"categoryId");
        }

        public function relatedCourses()
        {
            return $this->belongsToMany('App\Models\Course', 'relatedCourses', 'course1', 'course2');
        }

        public function originalCourses()
        {
            return $this->belongsToMany('App\Models\Course', 'relatedCourses', 'course2', 'course1');
        }
    }

Then you can query your relations,

$data["course"] = course::where("isActive",1)->with('relatedCourses')->find(5);

and

$data["course"] = course::where("isActive",1)->with('originalCourses')->find(7);

To merge both relations you can do like this

$course = course::where("isActive",1)->with('originalCourses', 'relatedCourses')->get();

Hope this will help you.

Chirag Patel
  • 1,545
  • 2
  • 9
  • 17
  • This will result in 2 different relations, i'm trying to set in 1 relation so i can do a foreach loop for $relatedCourses and not for both $relatedCourses and $originalCourses – Simos Fasouliotis Apr 06 '18 at 10:41
  • that will not work dear you have to do foreach loop for both check this question and answer [self many to many relations](https://stackoverflow.com/questions/17567305/laravel-many-to-many-self-referencing-table-only-works-one-way?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Chirag Patel Apr 06 '18 at 10:45
  • can at least we MERGE relatedCourses & originalCourses? – Simos Fasouliotis Apr 06 '18 at 10:52
  • I decide to use your technique and then manually merge the 2 results in 1. Thank you for your advice. – Simos Fasouliotis Apr 06 '18 at 10:58
  • Yeah. This is the best way to do :) – Chirag Patel Apr 06 '18 at 10:59
  • [click here](https://stackoverflow.com/questions/25049753/friendship-system-with-laravel-many-to-many-relationship) this will help you in eager loading. And I have updated my answer. Not sure but this query(with both relations eagar loading) should work. – Chirag Patel Apr 06 '18 at 11:01