R.Jonson
R.Jonson

Reputation: 55

Laravel Datatables display data from related tables

I'm using Datatables for displaying data. It works good, but when I want to display data from related tables (models), they do not appear.

Model Keyword.php

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

Model Website.php

 public function keywords() {
    return $this->hasMany('App\Keyword');
}

'websites' database table: id, siteName, siteUrl
'keywords' database table: id, website_id, kwName

Url that should display data: projects/'.$website->id.'/edit (example: projects/2/edit - (edit is a blade)) So, at this Url I want to show a datatable that display all kwName for certain website_id(in this example /2/).

Please, help me what should I use and how to do this.

Upvotes: 0

Views: 719

Answers (1)

Jeffrey
Jeffrey

Reputation: 473

Your Eloquent relationships are the wrong way around.

keywords has a reference to website via website_id, this is a belongsTo relationship.

website is referenced by many keywords, so this is a hasMany relationship.

Laravel 5.4 Eloquent Relationships

Upvotes: 1

Related Questions