数据透视表使用Laravel 5.7返回null

时间:2019-04-30 10:57:15

标签: php laravel laravel-5 pivot-table

我有两个models,名字是:联盟用户

有一个名为{strong> pivotleague_user表包含以下结构:

id
user_id
league_id
joined_at
rank
payment_id
created_at
updated_at

这是我的模特:

class League extends Model
{
    protected $fillable = [
        'name', 'capacity', 'is_open', 'started_at', 'finished_at', 'is_free', 'price', 'level', 'user_id', 'closed_by', 'edited_by'
    ];

    protected $hidden = [];


    /*
     * User Relationship
     */
    function user()
    {
        return $this->belongsTo(User::class);
    }


    /*
     * Editor Relationship
     */
    public function editor()
    {
        return $this->belongsTo(User::class, 'edited_by');
    }

    /*
     * All users Relationship
     */
    public function all_users()
    {
        return $this->belongsToMany(User::class)->withTimestamps()->withPivot('rank', 'joined_at');
    }
}

和用户模型:

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'family', 'email', 'mobile', 'password', 'username', 'team', 'email_verified_at', 'mobile_verified_at', 'role_id'
        ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    /*
     * Roles ralationship
     */
    public function role()
    {
        return $this->belongsTo(Role::class);
    }

    /*
     * Question Relationship
     */
    public function questions()
    {
        return $this->hasMany(Question::class);
    }

    /*
     * League Relationship
     */
    public function leagues()
    {
        return $this->hasMany(League::class);
    }


    /*
     * Setting Relationship
     */
    public function setting()
    {
        return $this->hasOne(UserSetting::class);
    }


    /*
     * All Leagues that User Joined
     */
    public function all_leagues()
    {
        return $this->belongsToMany(League::class)->withTimestamps()->withPivot('rank', 'joined_at');
    }

}

现在,当我想访问数据透视表中的rankjoined_at时,似乎出了点问题,或者至少我以错误的方式进行了操作。

我尝试过:

foreach ( $leagues as $league )
{
    $pivot[] = $league->pivot;
}

dd($pivot);
}

检查我的数据透视行为,我也检查了$league->pivot->rank$league->pivot->joined_at,但是pivot表似乎是 null

有人可以告诉我我的代码有什么问题吗?

我看到了以下链接:

laraveldaily

laracast

laravel documentation

和...

1 个答案:

答案 0 :(得分:0)

如果league_user表具有主键['league_id', 'user_id'],您应该可以访问类似的联赛

$user = user::find(1);
$leagues  = $user->leagues;

并可以遍历

@foreach($leagues as $league)
    {{ $league->rank }}
    {{ $league->joined_at }}
@endofreach

对于用户模型中的应该为

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function leagues()
    {
        return $this->belongsToMany('App\League');
    }
}

以及在联赛模型中

class League extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function users()
    {
        return $this->belongsToMany('App\User');
    }
}

推荐docs