user3566301
user3566301

Reputation: 172

How to getting rows from Laravel's belongsToMany relations?

I've problem with belongsToMany relation on Laravel.Firstly, I want to describe my table structe.There 3 different table has got on my database.Projects, Skills and ProjectSkills are my tables.There is a structure of my model.It's working perfectly when I tried to insert project with skills.However, it's not listing skills when I tried to get a single project.

ProjectController:

$Project = Projects::with('skills')->where('Projects.SefTitle','=',$ProjectSef)->first();
return View::make('Projects.view',array('Project'=>$Project));

Project(Model)

...
public function skills(){
    return $this->belongsToMany('Skills','ProjectSkills','ProjectID','SkillID');
}
...

View:

@foreach($Project->Skills as $Skill)
    {{$Skill->Title}} <br />
@endforeach

Database and Datasets:

ProjectSkills

Skills

Projects

Upvotes: 1

Views: 112

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

For primary keys that differ from the default Laravel uses (which is just id) make sure you define your primary key on every of your model classes:

class Projects extends Eloquent {
    protected $primaryKey = 'ProjectID';
}

Upvotes: 1

Related Questions