mercredi 25 mars 2015

How to use link table with Eloquent?


Vote count:

0




I have two models: Country and Grade wrote them as:


Country.php:



class Country extends Eloquent {
protected $fillable = [];

public function grades()
{
return $this->hasMany('Grade', 'title');
}
}


Grade.php:



class Grade extends Eloquent {
protected $fillable = [];

public function country()
{

return $this->belongsTo('Country');
}
}


when I try to get all grades for specific country:



return Response::json(Country::find($country_id)->grades());


But this would result into empty object {}


I am using many to many link table:



Schema::create('countries_grades', function(Blueprint $table)
{
$table->increments('id');
$table->integer('country_id');
$table->integer('grade_id');
$table->timestamps();
});


It seems that laravel is not using the above table, what is the correct way to achieve this query?



asked 18 secs ago







How to use link table with Eloquent?

Aucun commentaire:

Enregistrer un commentaire