Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ghaleon's avatar

Retrieving data from database

I have 3 tables:

professor
id
name

discipline
id
name

and one to link them

disc_prof
prof_id
disc_id
How do I retrieve data as an Inner Join ? Something like:

SELECT prof.name, prof.id, disc.id, disc.name 
FROM disc_prof AS dc INNER JOIN professor as prof ON prof.id = dc.prof_id
INNER JOIN discipline AS disc ON  dc.disc_id = disc.id  

What I did so far:

Professor Model

class Professor extends Model
{
    protected $table = "professor";

    public function disc()
    {
        return $this->belongsToMany('App\Discipline');
    }
}

Discipline Model

class Disciplina extends Model
{
    protected $table = "discipline";
}

But I'm getting the following error:

Base table or view not found: 1146 Table 'database.discipline_professor' doesn't exist

It's not database.discipline_professor, it's database.disc_prof.

What is the correct way to change it ?

0 likes
7 replies
Demers94's avatar

It's not database.discipline_professor, it's database.disc_prof. What is the correct way to change it ?

Try this :

 public function disc()
 {
     return $this->belongsToMany('App\Discipline', 'disc_prof');
 }
martinbean's avatar

@Ghaleon Your problem is you’re not using the table naming convention Eloquent expects for your pivot table.

From http://laravel.com/docs/master/eloquent-relationships#many-to-many:

The role_user table is derived from the alphabetical order of the related model names, and contains the user_id and role_id columns.

In your case, your table should be named discipline_professor. If you name it as such, your relationship will Just Work™.

Of course, you can supply additional parameters to override table and columns names in your relationships, but it’s just easier to use conventional names. In this case, your relationship method should be called disciplines() instead of disc(). The following is much more readable:

$disciplines = $professor->disciplines;
1 like
Ghaleon's avatar
Ghaleon
OP
Best Answer
Level 1

Well, here is the solution I got from Josh Janusch on stackoverflow.

ps: As @martinbean, Josh also highly recommended me to follow the Eloquent names. So that's what I'm going to do. As it's just a teste, I'll rename the database.

The work around for those who might need it:

Model::belongsToMany(), you will see the arguments are:

string $related, string $table = null, string $foreignKey = null, string $otherKey = null, string $relation = null
class Professor extends Model
{
    protected $table = "professor";

    public function disc()
    {
        return $this->belongsToMany('App\Discipline', 'disc_prof', 'prof_id', 'disc_id');
    }
}
martinbean's avatar

@Ghaleon It wasn’t illegal, but just think it’s a bit disingenuous to award yourself the answer when you arrived to it from others’ input (including mine as you so acknowledged).

Please or to participate in this conversation.