mardi 14 novembre 2017

Laravel 5.5 three table join

Edit: I am using API routes, so i have no views and such

I have a database like this

User:
   id
   name

card:
   id
   card_number
   user_id


swipe:
   id
   time
   card_number

the relationships go as followed user has one card one card has many swipe

the joins would be as such

user.id = card.user_id
card.card_number = swipe.card_number

In Larvel i have 3 models.

user
card
swipe

user Model

class user extends Model
{

    public function card()
    {
        return $this->hasOne('App\card','user_id','id');
    }

}

in a controller if i do

$model = user::with('card')->get();

i get the card data joined with the user data as expected.

class card extends Model
{


    public function person()
    {
       return $this->belongsTo('App\user');
    }

    public function swipe(){
        return $this->hasMany('App\swipe','card_number','card_number');
    }
}

if i now do

$model = user::with('card.swipe')->get();

which i would expect to return all the users with their cards, and swipes associated to that card.

However what i get is

500 internal server error

What am i doing wrong here?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2yASlhG
via IFTTT

Aucun commentaire:

Enregistrer un commentaire