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

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');
    }
}
0

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.

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.