Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Reread the beginning of the Eloquent docs and the part about protected $table and what happens when you don't define it.

Also should a repository be extending a model ??

0

The model has a protected property table. I didn't post the whole model class here to be easy to read and understand my problem.

0

Base table or view not found: 1146 Table 'genero_jogo_repository' doesn't exist

The part you don't have in the paste above is the definition of that relationship that is looking for that table to be the pivot. That should be the issue.

Last updated 7 years ago.
0

No. This is not the error at all. If I hadn't defined the relationship on the model file, Laravel would return other error message.

Last updated 7 years ago.
0

So are we going to pretend like that table exists?

0

I think you didn't understand the problem. I'm going to post the whole models if you insist.

Last updated 7 years ago.
0

App\Models\Jogo.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Jogo extends Model
{
    protected $table = 'jogos';

    public function generos()
    {
        return $this->belongsToMany(Genero::class);
    }
}

App\Models\Genero.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Genero extends Model
{
    protected $table = 'generos';

    public function jogos()
   {
        return $this->belongsToMany(Jogo::class);
    }
}

App\Repositories\JogoRepository.php

namespace App\Site\Repositories;

use App\Models\Jogo;

class JogoRepository extends Jogo
{
     ...
}
Last updated 7 years ago.
0
Solution

Yes I insist as the actual ERROR is directly related to the parts you left out :).

That relationship uses a 'pivot' table. What does Eloquent use as the pivot table name when you do not specify one in that relationship?

Last updated 7 years ago.
0

Thanks. I've insisted that was not the error, because Laravel automatically join the two related model names in alphabetical order. And I actually had that table in my database. When I used the model itself, everything worked fine! But when I used the repository class, Laravel misleaded the pivot name. And explicitly defining the pivot table name in the relationship, further the foreign keys, solved the problem when I use the repository class.

Last updated 7 years ago.
0

No problem. The automagic parts can bite you sometimes when its not obvious what they are doing. Good luck and have fun :).

0

Really appreciate it!

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.