Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

you use first because the query will return an array

Manufacturer::where( 'name', 'Ford' )->first()->models;

if you know the id, better yet

Manufacturer::find( 1 )->models;
Last updated 2 years ago.
0

Wow thank you so much!

For anyone else out there with a similar issue here is my final as I had some mistakes/typos in my classes:

$models = Manufacturer::where( 'manufacturer_name', 'ford' )->first()->relatedtoModel;

Class Manufacturer extends Eloquent {

protected $table = 'manufacturers';
protected $primaryKey = 'manufacturer_id';
protected $guarded = array('manufacturer_id');
protected $fillable = array('manufacturer_name');

public function relatedtoModel(){

	return $this->hasMany('Model','manufacturer_id');

}

}

Class Model extends Eloquent {

protected $table = 'models';
protected $primaryKey = 'model_id';
protected $guarded = array('model_id');
protected $fillable = array('manufacturer_id','model_name');

public function relatedtoManufacturer(){

	return $this->belongsTo('Manufacturer','manufacturer_id');

}

}

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.