Mass assignment enter value in guarded field - laravel-4

Is it possible to fill a guarded field with the ::create method from Elequent models,
If my User model is like so:
$guarded=['password','id']
$fillable=['username']
Is it possible to do this?
User::create(['username'=>'mynewusername','password'=hash::make($password)])

No - you cannot do that.
But you could do this
$user = User::create(['username'=>'mynewusername']);
$user->password = Hash::make($password);
$user->save();

Related

Laravel update one field of a model relation

I am trying to update only one field on a models relation but it keeps returning it as null and I cant figure out why;
The profile is a belongsTo relationship
$user = User::find(1);
$user->profile->api_key = 'apikeyhere';
$user->save();
After i do this and then run dd($user->profile) .. The api_key field is null instead of the actual value.
Using two query
$user = User::find(1);
$user->profile()->update([ 'api_key' => 'apikeyhere']);
Using One query
Profile::where('user_id', 1)->update([ 'api_key' => 'apikeyhere']);
Update the field using the relation.
$user = User::find(1);
$user->profile()->update(['api_key' => 'api_key value']);

How to change value in hasOne related table in Laravel?

I can't figure this out and I know it's so basic ;-)
I have two tables, user and userInfo. The relationship is one-to-one, very simple. How can I update userInfo? This is code from the end of my UserSeeder:
// after seeding, change the 1st user to always be the same
$user = User::first();
$user->name = 'admin';
$user->email = 'test#mysite.test';
$user->userInfo()->first()->AccountStatus = 1;
$user->save();
It runs without failing but userInfo doesn't set the user's AccountStatus = 1.
What am I missing?!! ;-)
Also: why do I have to do userInfo()->first() when there's always only one userInfo record connected to the user?
p.s. this is NOT a duplicate of How to update related table in Laravel? -- that's for many-to-many relationship... this is one-to-one.

How to except some element from collection Laravel?

I do request to Model:
$user = User::findOrFail($id)->get();
And afetr try to except some fields from $user collection:
$user = $user->except(['surname', 'is_buyer', 'password']);
But in result I get still full $user collection
The problem here is that you don't have here collection as you expect. You have here collection of models, so you will get all model fields. Also using:
$user = User::findOrFail($id)->get();
is really strange, as you will get always 1 record and put this into collection.
Depending on your needs you might want to choose only some fields from database like this:
$user = User::select('id', 'name')->findOrFail($id);
instead of using collections here.

How to get row in Laravel?

I get one row by $id in Laravel like as:
$user = User::where("id", $id)->get();
After I need to do return $user->first();
Can I use this request more shorty?
You can simply run:
return User::find($id);
or maybe (depending on your needs) even:
return User::findOrFail($id);
you donot have to call ->get() and ->first(), since you are sure that you are going to get only one result by the id column, search like this,
User::find($id);
or
User::where('id',$id)->first()
and you have other alternative methods in the case you want to handle exceptions, ->findOrFail() or ->firstOrFail()
You can use $user = User::findOrFail($id); since the id is unique so you'll either get a User object or Exception which you can catch and respond accordingly.
Since you have only one where clause, the shortest way will be
return User::find($id);

Laravel 5 Modify Mass Assignment

How I can change the fillable attribute of a model on the fly?
For example, I have User model with,
protected $fillable = ['name', 'email', 'password']
When updating the user, I want to exclude 'email' from mass assignment so that the email is not changed on update.
Mass assignment doesn't mean all the field listed in fillable will be auto filled.
You still have control over what to save in the table.
So if you do:
$user = User::find(1);
$user->email = 'email#emails.com';
$user->save();
Only email will be saved in the example above while name and password remains the same

Resources