Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 1 year ago.
0

Ok, I managed to get round this by using the findOrNew() function, assigning the values for employee_id and section_id on the new object created then calling save().

I think this is better practice as foreign keys shouldn't be mass assigned as far as I can make out. Visible, hidden, fillable, guarded with Foreign Keys

Might be of use to someone having a similar problem with create() or findOrCreate() in the future.

Last updated 1 year ago.
0

I have the same issue as you did, but when I use the findOrNew() function I don't get any kind of error, but it doesn't create a new record in the database.

I'm trying to update a profile of a user and if the profile isn't there create it.

$profile = Profile::findOrNew(['user_id' => $update->id], $cleanInput);

I get the following error with the above code

Column not found: 1054 Unknown column '[email protected]' in 'field list'

I have also tried firstOrNew with no luck.

$profile = Profile::firstOrNew(['user_id' => $update->id])->update(array_except($cleanInput, [
			'email',
			'alias',
			'username',
			'permissions',
			'last_login',
			'shooter_uuid',
			'first_name',
			'last_name',
			'password',
			'include',
		]));

The above just returns the ID of the last profile

If I don't use array_except on the input data I get the following error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'field list' 

How did you assign your variables and then save the data to the new record?

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jol-bdcons jol-bdcons Joined 23 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.