mardi 13 septembre 2016

Laravel 5.1 belongs to many not working

I have following tables.

Users

id

name

Events

id

name

Cards

id

name

Transfers

id

event_id

card_id

I added the belongs to relationship in the Card.php as well as in Event.php

class Card extends Model
{
 public function user()
    {
        return $this->belongsTo(User::class);
    }

     public function events()
    {
        return $this->belongsToMany(Event::class,'transfers');
    }


}

class Event extends Model
{
        use SoftDeletes;

        protected $dates = ['deleted_at'];


    public function user()
    {
        return $this->belongsTo(User::class);
    }
    public function cards()
    {
        return $this->belongsToMany(Card::class,'transfers');
    }





}

I was trying to use the following statements in my controller both of them returned error

> echo count($user->events->cards->where([['id', '=',
> '57']])->find());die; //$cards is not defined.


> echo count($user->events->cards()->where([['id', '=',
> '57']])->find());die; // method cards() is not defined.I tried this after reading a tutorial

Any help on resolving this issue is appreciated.

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire