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 ??
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.
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.
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.
I think you didn't understand the problem. I'm going to post the whole models if you insist.
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
{
...
}
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?
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.
No problem. The automagic parts can bite you sometimes when its not obvious what they are doing. Good luck and have fun :).
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community