Hello,
##Tables structure
###Users
id - AI
password - string
email - string
userable_id - integer
userable_type - string
created_at - datetime
updated_at - datetime
###Admins
id - AI
first_name - string
last_name - string
###Clients
id - AI
company_name - string
##Polymorphic relationships
###User Model
public function userable()
{
return $this->morphTo();
}
###Admin Model
public function user()
{
return $this->morphOne('User', 'userable');
}
###Client Model
public function user()
{
return $this->morphOne('User', 'userable');
}
##Static factory method
###User Model
public static function createUser($type, array $userAttributes, array $typeAttributes)
{
if(class_exists($type))
{
$userType = $type::create($typeAttributes);
$userType->account()->create($userAttributes);
return $userType;
}
else
{
throw new Exception("Invalid user type");
}
}
##Testing
###routes.php
Route::get('/', function() {
User::createAccount(
'Admin',
[ 'email' => 'example@email.com', 'password' => Hash::make('1234') ],
[ 'first_name' => 'John', 'last_name' => 'Doe' ]
);
});
##Question
It's working, but i'm wondering if i'm doing it the right way?
Hi again,
I'm trying to use Confide and Entrust. Now my User Model extends ConfideUser and uses the HasRole Trait. I added a Role Model (extends EntrustRole), a Permission Model (extends EntrustPermission) and corresponding tables. I also updated the static factory method inside my User Model.
##Static factory method
###User Model
public static function createUser($type, array $userAttributes, array $typeAttributes)
{
if(class_exists($type)) {
$role = \Role::where('name', '=', $type)->first();
$userType = $type::create($typeAttributes);
$user = $userType->user()->create($userAttributes);
$user->attachRole($role);
return $accountType;
} else {
throw new InvalidAccountTypeException('Invalid account type.');
}
}
Is there a better way to do this ?
Can someone explain to me how morph works?
What I mean is, I don't know what fields are required in my tables to get it working.
I see you are using userable_id
and userable_type
. Are these necessary?
I fail to understand how I should make my table constraints.
UDPATE: Silly me, I seem to have missed the part in the docs about the table structure. I think I understand it now.
I am new to Laravel and trying to create an application with different types of users. The users have both common and unique characteristics. After looking at Single Table Inheritance models, I decided to try polymorphic relations. Most of the examples deal with retrieving the records. This is the first post I found with an example of how to create new (different type) users.
The problem is, I am too inexperienced to fill in the blanks for all of the above code. I am confused, for example, by the label User Model being used under Polymorphic relationships and again under Static factory method. What should the file name be for the factory if it is a separate file, and where should it be saved in the folder structure? Could you post the complete scripts or a link to the examples above as a learning aide? Thank you.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community