lundi 2 mai 2016

Query builder on hasMany relation

Problem

I'm creating an API with Laravel. Every server can have more than one contact, and every contact belongs to one server - as such, contacts are set up with a belongsTo relationship, and servers have a hasMany relationship to the contacts.

A user can have many servers via its hasMany relationship. Thus, when creating a server, we simply invoke $user->server()->create([values]), which works just fine.

The issue is that when we try to invoke it further, as with contacts - we hit a wall where we get:

Call to undefined method Illuminate\Database\Query\Builder::contacts()

When using: $user->server()->contacts()->create([]).

The method does exist in the server model. We also have a hasManyThrough relationship on the user model, specifying that a user has many contacts through servers.

When calling $user->contact()->create([]), we instead get:

Call to undefined method Illuminate\Database\Query\Builder::create()

Does anyone have a clue what might be causing this? Do query builders not allow for this type of chaining, or am I missing something blatantly obvious? As you can see, I've tried a few different methods but can't seem to produce a working result.

Cheers!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire