Well you can create a join with Eloquent! Try the documentation: http://laravel.com/docs/5.1/queries#joins
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 ?
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');
}
}
Please or to participate in this conversation.