Simon
Simon

Reputation: 381

Eloquent not linking tables

So I've got 3 tables. But Laravel fails to link the tables to eachother, I feel like I'm missing a really small thing but I can't seem to find it.

Controller

$data = Item::all();
$clipper = $data->first();
dd($clipper->attributes());

Item

protected $table = 'item';
protected $primaryKey = 'item_id';
public $timestamps = false;

public function attributes() {
    return $this->hasMany('App\Attributes', 'item_id');
}

Atrributes

protected $table = 'attributes';
protected $primaryKey = 'attr_id';
public $timestamps = false;

public function name() {
    return $this->hasOne('App\Attributesname', 'name_id');
}

Attributes Name

protected $table = 'item_attributes';
protected $primaryKey = 'item_attr_id';
public $timestamps = false;

Result of dd

Result of dd

Database design

Design of my database

Upvotes: 0

Views: 67

Answers (1)

fico7489
fico7489

Reputation: 8560

Laravel links relations fine, see you output parent model is the Item, related model is the Atribute, try this to see related data (not relation) :

$data = Item::all();
$clipper = $data->first();
dd($clipper->attributes);

Upvotes: 1

Related Questions