just like any other eloquent object $user->id
Get id of newly created record
Hi folks,
If you create a new record so:
$user = User::create($request->only('email', 'name', 'password')); //Retrieving only the email and password data
How to then grab hold of that record to add something to it?
as @vajid already mentioned, easiest would be:
$id = $user->id
but you could do a query as well, eg:
$id = User::orderBy('id, 'desc')->first();
or with raw SQL, using lastInsertId()
Hmm, I'm missing something:
$id = $user->id;
//Checking if a role was selected
if (isset($roles)) {
foreach ($roles as $role) {
$role_r = Role::where('_id', '=', $role)->get()->pluck('name');
$user->assignRole($role_r); //Assigning role to user
}
}
Role is not updated for user
How about:
$user->roles()->sync($roles);
Asusming the User model has a :
public function roles()
{
return $this->belongsToMany(Role::class);
}
The reason I'm befuddled is I have an update method that successfully assigns role to the user so:
if (isset($roles)) {
foreach ($roles as $role) {
$role_r = Role::where('_id', '=', $role)->firstOrFail();
$user->assignRole($role_r); //Assigning role to user
}
}
Using the same code in the store method just does not work, I even added a flash message to print out $role_r:
flash($role~_r)->success();
and it prints out the correct value, it's as I stated in my opening post how do I get a proper hold on the $user variable after it is created?
That is not the problem....
$user = User::create($request->only('email', 'name', 'password'));
// $user is now a User object
// so either do it the right way
$user->roles()->sync($roles);
// or do it the hard way and loop over everything
foreach ($roles as $role) {
// but why are you using id fields called _id ?
$role_r = Role::where('_id', '=', $role)->firstOrFail();
$user->assignRole($role_r);
}
We have no idea what assignRole is actualy doing behind the scenes.
The sync way should be working, but please.... to make your own life easier.... stick to naming conventions, it keeps you from getting into weird and hard to solve bugs.
Doing it the easy way, following https://github.com/mostafamaklad/laravel-permission-mongodb :
$user->roles()->syncRoles($roles);
results in :
BadMethodCallException
Call to undefined method Jenssegers\Mongodb\Relations\BelongsToMany::syncRoles()
So I guess the only solution is to loop through the array:
if (isset($roles)) {
foreach ($roles as $role) {
$role_r = Role::where('id', '=', $roles)->get()->pluck('name');
$user->assignRole($role_r);
flash('I am in the loop,'. $role_r)->success();
}
}
The flash line prints out "I am in the loop,[]" so it looks like the Role & Permissions package has blocked the query, dunno what to try next!
@lungo, you migth have mentioned you were using MongoDB and an external package ;)
That kinda changes everything.....
But again, first try what i actually posted:
$user->roles()->sync($roles);
// is not the same as
$user->roles()->syncRoles($roles);
$role_r = Role::where('_id', '=', $role)->get()->pluck('name');
$user->assignRole($role_r); // if assignRole accepts array of role name
or
$role_r = Role::where('_id', '=', $role)->first();
$user->assignRole($role_r->name); // if assignRole accept single role name
ok, i'm done..... have fun
Please or to participate in this conversation.