Creation of multiple instance of an object - php

I am currently learning laravel and stuck with an assignment. I have the program where User will have multiple Whiteboards where he can post his notes. What i want to achieve is when User is created it should create 4 different Whiteboards.
This is working for 1 Whiteboard creation but not sure how I can achieve 4 while creating user. I have relationship set up where each user can have multiple Whiteboards and each Whiteboard will have only one User.
protected function create(array $data)
{
$whiteboard = Whiteboard::create([
'username' => $data['username'],
'name' => $data['name'],
]);
return User::create([
'email' => $data['email'],
'password' => bcrypt($data['password']),
'whiteboard_id' => $whiteboard->id,
]);
}
I expect something like this (Just dummy example) -
User A should have whiteboards created with ID 1,2,3,4
User B should have whiteboards created with ID 5,6,7,8

You can do somethings like this,
protected function create(array $data)
{
$user = User::create([
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
for($i=0; $i<=3; $i++)
{
$whiteboard = Whiteboard::create([
'username' => $data['username'],
'name' => $data['name'],
'user_id' => $user->id
]);
}
return $user;
}
In that case you have to change your relationship User (one) Whiteboard (many)

Related

LaravelTrust and LaravelAuthUI can't import role to database

I have a problem with LaraTrust $User->attachRole('user'), and Laravel Auth UI. I want to attach a role upon registering a new User. After the registering, I don't see any data in the roles table.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'phonenumber' => $data['phonenumber'],
'password' => Hash::make($data['password']),
]);
$User->attachRole('user');
}
You are directly returning the created user before attaching the role.
Either:
$user = User::create([]);
$user->attachRole('user');
return $user;
Or
return User::create([])->attachRole('user');

Assigning user role upon registration in laravel issue

In my laravel application i'm trying to assign user role upon the registration. Following is my RegistrationController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'mobile'=>$data['mobile'],
'username'=>$data['username'],
'password' => Hash::make($data['password']),
'propic'=>'user-pic.png',
'user_roles' =>'customer',
]);
$lastdata = DB::table('users')->orderBy('id', 'desc')->first();
$last_id=$lastdata->id;
$user= User::where('id','=',$last_id)->firstOrFail();
$user->assignRole('customer');
}
But when I run the code user get created, activation mail has been sent but the role is not assigning! how can i fix that and where am I doing wrong?
There are a few things you might want to consider.
1) your function won't proceed further after the return statement. You are creating a user with a return statement hence your code below of that won't execute. I'll recommend storing the return object into some variable.
// let's say
$user = User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'mobile'=>$data['mobile'],
'username'=>$data['username'],
'password' => Hash::make($data['password']),
'propic'=>'user-pic.png',
'user_roles' =>'customer',
]);
// then you can simply get it's value by
$user_id = $user->id; // you won't need to query last added row's value.
2) If the user role is not getting saved into your database then check if you have assigned that 'user_roles' field as fillable into User model.

How to change User::Model In Laravel with User Auth in Firebase

Recently I have make Laravel auth with composer require laravel/uiand I can edit/create user with MySQL where as auth users for Firebase .
I also can create/edit Users for each database and everything is OK for now, but what I just want is to create a new user form Laravel {RegisterController}
I can create the user with Firebase, but what value or what object shall I return to be compatible with Laravel auth?
Create User with Laravel
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
Create User with FireBase
$userProperties = [
'email' => $data['email'],
'emailVerified' => false,
'password' => Hash::make($data['password']),
'displayName' => $data['name'],
];
$user = new AuthController();
return $user->Create_User($userProperties);
AuthController Class
public function __construct()
{
$this->serviceAccount=ServiceAccount::fromValue(__DIR__.'/FirebaseKey.json');
$this->firebase=(new Factory)->withServiceAccount($this->serviceAccount);
$this->auth=$this->firebase->createAuth();
}
public function Create_User($userProperties=[]){
return $this->auth->createUser($userProperties);
}
return value with dd(); -->User(Laravel)
return value with dd(); -->User(Firebase)

Laravel create multiple user tables

I have two tables - users and user_attributes.
During registration user I would like to not only enter the user in the user table but also create his attributes.
AuthController
protected function create(array $data)
{
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return UserAttribute::create([
'user_id' => $data['id'], // Undefined index: id
'lvl' => 0,
'exp' => 0,
'currency' => 500,
'premium_currency' => 5,
]);
}
How do I get user_id?
PS: I'm sorry for my english.
Do this
$user =new User;
$user->thing1 = 12;
$user->thing2 =32;
$user->save();
And now as soon as you have called the save method you can get the id by doing
$user->id;
Remember! It will only work after you have called the save method.
Also remember to replace my data with your data.
For further info and explanation see here: https://laravel.com/docs/5.2/eloquent#basic-inserts
Best of luck!

Assign new user a plan upon registration in Laravel 5

When a new user registers, they should be automatically assigned to a plan subscription. I can do that manually in Tinker (Laravel 5):
$token = Input::get('stripeToken');
$user = User::all();
$user->subscription('monthly')->create($token);
flash('Your account has been created with a membership');
Where, in Laravel 5, do I put such logic?
Edit
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'company_name' => $data['company_name'],
// I have added the below:
$token = Input::get('stripeToken');
$user = User::all();
$user->subscription('loop')->create($token);
]);
}
If you are using Laravels Registrar service then I'd do it in there. That could look like this:
public function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'company_name' => $data['company_name']
]);
$token = Input::get('stripeToken');
$user->subscription('loop')->create($token);
return $user;
}
You might also want to get the stripeToken from the $data array but I'll leave that to you.

Categories