[Laravel] Quick Tip: Saving Related Models with Eloquent

Recently, I encountered some difficulty in saving related models with Eloquent ORM.  I spent quite a bit of time trying to figure out why in one area of my app it saved with no problem but in another, it wouldn’t save the key/val pairs I passed to it.

Finally, (by a bit of trial and error) I realized that the call I was making to Model::accessible (a type of security feature that allows you to specify the accessible fields in a model) was, for some reason, preventing the related models from insert/updating. Once I commented that out it worked like a charm. (Note: I did go back and manually specify the fields the model should insert/update to avoid potential security problems with mass assignment).

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: