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

armancs's avatar

Join three table and get specific data

I have three table called country , university and university info. I want, if university info exist than country will display in my view file which is reltaed with university table and university table is related with university info.i dont need to access region id.

universityinfomodel:

protected $table = 'university_info_tbl';
protected $primaryKey = 'id';
    protected $fillable = ['university_id','des'];

public function cities() {
    return $this->belongsTo('App\Model\RegionModel');
}
public function country() {
    return $this->belongsTo('App\Model\CountryModel','ref_country_id');
}
public function university() {
    return $this->belongsTo('App\Model\UniversityModel','university_id');
}

UniversityModel:

protected $table = 'university_tbl';
protected $primaryKey = 'id';
    protected $fk       = ['ref_country_id'];
protected $fillable = ['ref_country_id','region_id','name','url'];
public function country() {
    return $this->belongsTo('App\Model\CountryModel');
}

CountryModel:

protected $table = 'countries_tbl';
protected $primaryKey = 'id';
    protected $fillable = ['name'];
public function university() {
    return $this->hasMany('App\Model\UniversityModel');
}

i tried to get country from university table i got it but in university there is multi country id which is not my goal.i want to one country in every university.

i use this

$countries->country->name

Please help . Thanks

0 likes
4 replies
automica's avatar

If you use standard Laravel table and field names you’ll have a much easier time getting this to work.

Firstly, your tables don’t need the _tbl suffix - they are already tables in a db.

Your tables can just be ‘countries’ and ‘ universities’

See https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md#follow-laravel-naming-conventions

Secondly, your ref_country_id should be just country_id. This way you don’t need to define the $fk. Also given you’ve already got region_id in that table, the region_id is also a foreign key so a variable named $fk isn’t the only one.

In your view, if you’ve got a single university you should be able to display the country as such

$country = $university->country->name;

Also, to follow convention, you don’t need to suffix Model onto the end of your model names. Follow conventions, and it’ll make all your dev easier (and much easier for others to debug).

armancs's avatar

I made table manually for this define table.If i don't define it without migration is it ok? @brightstormhq thanks for your quick reply. my question getting change.have you time to look it.

automica's avatar

@armancs you'd be better create the table with a migration file to make it easier to deploy your application.

I'm not sure how your question has changed but i can see the addition of

$countries->country->name

which won't get you anything as there isn't a relationship to country on your $country model.

If you can look at your question again and clarify then i'll have another go at answering,

armancs's avatar

Countr and university table have relation when i retrive country according to university it display country list.but problem is joining three table

Please or to participate in this conversation.