Laravel 未找到枢轴 table

Laravel Pivot table not found

我的目标。 我有 3 个 Laravel 模型:usercampaigncampaign_players。我想获取有关用户的信息,以及正在播放的活动 he/she。

问题。 当我 运行 这个查询时:

$campaigns = user::with(['campaigns_playing'])->where('id', $id)->get();

它给了我这条信息:

"Base table or view not found: 1146 Table 'sagohamnen.campaign_players' doesn't exist (SQL: select sh_campaigns.*, campaign_players.user_id as pivot_user_id, campaign_players.id as pivot_id from sh_campaigns inner join campaign_players on sh_campaigns.id = campaign_players.id where campaign_players.user_id in (2))"

由于某种原因,它似乎找不到 table 名称。你们知道导致问题的原因以及解决方法吗?

这是我的模型及其数据库 tables.

中的短代码片段

用户

protected $table = 'sh_users';
public function campaigns_playing()
{
        return $this->belongsToMany('Sagohamnen\campaign\campaign', 'Sagohamnen\campaign\campaign_players', 'user_id', 'id');
 }

Schema::create('sh_users', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->string('name', 255);
});

活动

protected $table = 'sh_campaigns';

Schema::create('sh_campaigns', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->string('name', 255);               
        $table->timestamps();
});

Campaign_players

protected $table = 'sh_campaign_players';

Schema::create('sh_campaign_players', function (Blueprint $table) {
        $table->integer('user_id')->unsigned();
        $table->integer('campaign_id')->unsigned();
        $table->date('date');
        $table->primary(['user_id', 'campaign_id']);
});

将枢轴 table 名称作为第二个参数传递给 belongsToMany() 方法。它要求输入字符串,而不是模型 class 路径。

Sagohamnen\campaign\campaign_players 更改为 sh_campaign_players(或者数据库中的枢轴名称 table)