0
votes

My goal. I have 3 Laravel models: user, campaign and campaign_players. I want to get info about a user, and also the campaigns he/she is playing.

The problem. When I run this query :

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

it gives me this message:

"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))"

It looks like it can't find the table name, for some reason. Do you guys have any idea what causing the problem and how to fix it?

Here are short code pieces from my models and it´s database tables.

User

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);
});

Campaign

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']);
});
1
it looks like there may be a few things wrong here. The first error you get is that table campaign_players doesn't exist. From the code you have posted it seems you have named the table sh_campaign_players.tam5
Thanks for commenting. Yeah, I wrote the name of the model, called campaign_players. You mean I should write the MySql table name instead of the model?Olof84
what @Ruffles said is correct about how to pass the name of the table. In addition I would encourage you to read the Laravel docs. There are certain conventions which are encouraged. Such as calling the pivot table campaign_user in this case.tam5
Thanks. I considered changing it to campaign_users, though I thought campaign_player would be more logical, for my purpose.Olof84
I certainly agree that naming things to fit your case is a good idea. Since it is a Laravel convention though, if you were to use campaign_user for example, you would only need to pass one argument to your belongsToMany and Laravel will imply the rest, rather than you needing to be explicit about everything. [And you can still name the methods however you see fit like campaignsPlaying]. But again, just a suggestion, do what makes sense for your case :)tam5

1 Answers

1
votes

Pass the pivot table name as second parameter to the belongsToMany() method. It asks for a string, not a model class path.

Change Sagohamnen\campaign\campaign_players to sh_campaign_players (or whatever the name of your pivot table is in the database)