public function states() { return $this->belongsToMany( State::class, // Related model 'user_state', // Pivot table name 'user_id', // Foreign key on the pivot table (points to User) 'state_abbrev', // Foreign key on the pivot table (points to State) 'id', // Local key on the User model 'state_abbrev' // Local key on the State model )->withPivot('state_abbrev'); // include any info from the pivot table }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter