There is a championship_user pivot table:

Table

I get the User 's Championship data like this:

 public function championships() { return $this->belongsToMany(Championship::class)->withPivot('skill_id', 'role', 'result'); } 

In the controller, I need to get all the user's Championship :

 $championships = Auth::user()->championships(); $skills = Skill::all(); dd($championships); 

The output of dd() is:

 #related: Championship {#236 ▼ #hidden: [] #guarded: [] #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: false +wasRecentlyCreated: false #attributes: [] #original: [] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #visible: [] #fillable: [] } 

Those. he cannot find any records, although there are records:

table

What could be the error? How to fix?

    1 answer 1

    There was an error in my code: $championships = Auth::user()->championships(); . There should not be () at the end. You could leave it like that, but then you need to add ->get() like this: $championships = Auth::user()->championships()->get(); . Those. we create a request, but do not execute it.

    • one
      That's right, when calling the relationship method, the builder is returned. - Maxim