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

Because it doesn't use Model s method but HasOneOrManys. You operate on relation, not on the model.

This is going to do what you expect:

$husband->wife->create($anotherData); // Model method
while..
$husband->wife()->create($anotherData); // ..relation (HasOneOrMany) method

Because of dynamic properties of Eloquent:

$husband->wife(); // returns relation
$husband->wife; // returns Model (dynamic property here)
Last updated 2 years ago.
0

Using that principle, I got this error:

Call to a member function create() on a non-object 

Because:

var_dump($husband->wife); //NULL

Is there a code that I missed putting in so $husband->wife becomes an instance of Wife model?

Last updated 2 years ago.
0

Dynamic property returns always related Model or null depending if the relation exists or not. So it seems that this husband has no wife in fact ;)

Of course the relation itself could be defined wrong. Make sure it's ok first.

Last updated 2 years ago.
0

Sorry, I revised my codes to better explain what I am trying to do. Can you help me figure out what I don't understand from there?

Last updated 2 years ago.
0

Let's start with:

// belongsTo / hasOne
$model->relation; // returns related model instance or null if no relation found

$model->relation(); // returns belongsTo / hasOne object

Now, hasOne object has its own create method, and this is what you call doing

$husband->wife()->create();

while you want to do:

$husband->wife->create();

The latter will work as long as there is related model found (ie. one has wife), so first check for null:

if ( count ($husband->wife) )
{
   $husband->wife->create;
}

count is handy for any relation type, for more info check this http://stackoverflow.com/questions/23910553/laravel-check-if-related-model-exists#answer-23911985

However this is not going to make created wife model related to the husband, so in the end no point doing it that way..

I don't know what you want to achieve in that overriden method, but I suggest this pretty straightforward way:

$wife = Wife::create($attributes);
$husband->wife()->save($wife);
Last updated 2 years ago.
0

Thanks. The reason for trying to override the built-in create() method is to insert validation vodes before doing the actual record creation for both Husband and Wife models.

I don't want to put the validation to save() because of some reasons (and I don't want to create listeners in order to make the codes simple and straightforward.)

BTW, I'm not trying to find any matching record (because both the Husband and Wife records are both still yet to be created by the new create method so there is nothing to match.)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jhourlad jhourlad Joined 16 Jun 2014

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.