mpociot / teamwork

User to Team associations with invitation system for the Laravel 5 Framework
MIT License
1.07k stars 170 forks source link

hasMany relationship issues... #82

Closed marksie1988 closed 7 years ago

marksie1988 commented 7 years ago

Hey there,

I am currently trying to setup a one to many relationship between my "social" table and the "team" table.

I have done the following: my social table has the following columns: id, provider, oauth_key, oauth_secret, team_id

in my Team.php Model:

<?php namespace App;

use Mpociot\Teamwork\TeamworkTeam;
use HasRoles;

class Team extends TeamworkTeam
{
    public function social(){
        return $this->hasMany('App\Social');
    }
}

in my Social.php Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Social extends Model
{
    public function team(){
        return $this->belongsTo('App\Team');
    }
}

then on the Team index call dd($teams) but there is no relationship shown for the social table. I am not sure why it isnt picking up the relationship from the models, is there something im missing? according to the Laravel documents i should only need to add to the models to form the relationship?

jlndk commented 7 years ago

dd($teams->social);?

Laravel should try to save time, so you have to explicitly call the relationship or eager load it

marksie1988 commented 7 years ago

Unfortunately that doesn't seem to work, when i call $teams->social it states that the property doesn't exist on the collection

marksie1988 commented 7 years ago

Ok I have fixed it, for some reason it needed me to mention the local and foreign key even though I was using the laravel standard naming :)