The second argument should be "s_id"
class Artist extends Model {
protected $table = 'contactsdata';
protected $primaryKey = 'c_id';
public function artworks() {
return $this->hasMany('App\Item', 's_id', 'c_id'); // (Model, foreign_key, local_key)
}
}
Also, you should add the belongsTo relationship
class Item extends Model {
protected $table = 'stock';
protected $primaryKey = 's_id';
public function artist() {
return $this->belongsTo('App\Artist', 'your_foreign_key');
}
}
Thanks. I have actually solved this myself and it is an interesting issue so others may be interested. By the way, othmanus, you are right that belongsTo is needed, though its absence does not cause this problem. You are wrong about the second argument, the link is correct c_id->c_id. What is wrong is that Eloquent relationship field names are case sensitive. For some reason my database table has all its fields (in its scheme) named in UPPER CASE. Once I changed the hasMany parameters to 'C_ID', 'C_ID' everything worked just fine.
So worth knowing that Eloquent relationship field names are CASE SENSITIVE.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community