How to extend a Users model with another many to many relationship models Skills/Levels?Many-to-Many Eloquent relationship update with Laravel Form Model Binding & CheckboxesEloquent many-to-many-to-many - how to load distant relation easilyLaravel 5 - access specific model on many to many relationshipquerying many-to-many-relationship laravelLaravel 5 return column values in one to many relationshipRetrieve all models that are not associated with another model through pivotLaravel Many to Many - One Entry for all RelationshipsMany to many Laravel Eloquent relationship with multiple intermediate tablesTransform Raw Union Query into Eloquent RelationshipArray with single attribute of many-to-many Eloquent relationship
Why was Pan Am Flight 103 flying over Lockerbie?
Is it OK to say "The situation is pregnant with a crisis"?
How useful would a hydroelectric power plant be in the post-apocalypse world?
Five 5-cent coins touching each other
Find the closest three-digit hex colour
Rear derailleur got caught in the spokes, what could be a root cause
Why didn't Caesar move against Sextus Pompey immediately after Munda?
How does mmorpg store data?
Does "boire un jus" tend to mean "coffee" or "juice of fruit"?
Having to constantly redo everything because I don't know how to do it
What are the children of two Muggle-borns called?
Installed software from source, how to say yum not to install it from package?
What is my external HDD doing?
Avoiding repetition when using the "snprintf idiom" to write text
Any Tips On Writing Extended Recollection In A Novel
Which high-degree derivatives play an essential role?
Why would Dementors torture a Death Eater if they are loyal to Voldemort?
Do electrons really perform instantaneous quantum leaps?
What was the point of separating stdout and stderr?
Why are examinees often not allowed to leave during the start and end of an exam?
Simplify the code
"nunca" placement after a verb with "no"
Reaction mechanism of rearrangement
Is it theoretically possible to hack printer using scanner tray?
How to extend a Users model with another many to many relationship models Skills/Levels?
Many-to-Many Eloquent relationship update with Laravel Form Model Binding & CheckboxesEloquent many-to-many-to-many - how to load distant relation easilyLaravel 5 - access specific model on many to many relationshipquerying many-to-many-relationship laravelLaravel 5 return column values in one to many relationshipRetrieve all models that are not associated with another model through pivotLaravel Many to Many - One Entry for all RelationshipsMany to many Laravel Eloquent relationship with multiple intermediate tablesTransform Raw Union Query into Eloquent RelationshipArray with single attribute of many-to-many Eloquent relationship
I am trying to extend a User model with associated Skills and Levels. Where all users will have different skill levels for a variety of skills. Im trying to figure out the most efficient way to setup the relationships to accomplish this.
Im currently setting Skill to Level as a many to many relationship. User to skills_has_levels is one to many but of no current use.
There are many skills a user can have and each skill can have a different level from no experience to expert.
[Tables]
users
id, username, email, pwd, token, timestamps
skills (ie. laravel, angular, bootstrap, etc)
id, name, order
levels (ie. none, beginner, intermediate, expert)
id, name, value
skills_has_levels
id, skills_id, levels_id, users_id
We have three models:
1) User (has many skills with levels)
2) Skill (has many levels)
3) Level (has many skills)
In the Skill model: (to have the users_id available: unused)
$this->belongsToMany('AppLevel’)
->withPivot(‘users_id’);
To list all users, their skills, and levels :
$users = User::all()->toArray();
foreach($users as $user)
echo "User ID: ".$user->id;
$skills = Skill::where(‘users_id’, ’=‘, $user->id)->get()->toArray();
print_r($skills);
echo "nnn";
Ultimately, I would want the User model to return a skills/levels array of all the associated user skills & their levels: skill name, skill order, level name and level value.
What I currently do works but i feel it could be done better.
whats the best way to structure the relationships for the User model to connect to skills names and the associated levels values?
Is it possible to related the Skill/Level directly to the User model? So i only have to use the User model to access all of the associated skill and levels.
Or are my relationships wrong? Could change the models to this relationship:
A User has many Skill
Skill has many skill types
Skill has many skill levels
Thanks in advance.
php laravel laravel-5 eloquent
add a comment |
I am trying to extend a User model with associated Skills and Levels. Where all users will have different skill levels for a variety of skills. Im trying to figure out the most efficient way to setup the relationships to accomplish this.
Im currently setting Skill to Level as a many to many relationship. User to skills_has_levels is one to many but of no current use.
There are many skills a user can have and each skill can have a different level from no experience to expert.
[Tables]
users
id, username, email, pwd, token, timestamps
skills (ie. laravel, angular, bootstrap, etc)
id, name, order
levels (ie. none, beginner, intermediate, expert)
id, name, value
skills_has_levels
id, skills_id, levels_id, users_id
We have three models:
1) User (has many skills with levels)
2) Skill (has many levels)
3) Level (has many skills)
In the Skill model: (to have the users_id available: unused)
$this->belongsToMany('AppLevel’)
->withPivot(‘users_id’);
To list all users, their skills, and levels :
$users = User::all()->toArray();
foreach($users as $user)
echo "User ID: ".$user->id;
$skills = Skill::where(‘users_id’, ’=‘, $user->id)->get()->toArray();
print_r($skills);
echo "nnn";
Ultimately, I would want the User model to return a skills/levels array of all the associated user skills & their levels: skill name, skill order, level name and level value.
What I currently do works but i feel it could be done better.
whats the best way to structure the relationships for the User model to connect to skills names and the associated levels values?
Is it possible to related the Skill/Level directly to the User model? So i only have to use the User model to access all of the associated skill and levels.
Or are my relationships wrong? Could change the models to this relationship:
A User has many Skill
Skill has many skill types
Skill has many skill levels
Thanks in advance.
php laravel laravel-5 eloquent
add a comment |
I am trying to extend a User model with associated Skills and Levels. Where all users will have different skill levels for a variety of skills. Im trying to figure out the most efficient way to setup the relationships to accomplish this.
Im currently setting Skill to Level as a many to many relationship. User to skills_has_levels is one to many but of no current use.
There are many skills a user can have and each skill can have a different level from no experience to expert.
[Tables]
users
id, username, email, pwd, token, timestamps
skills (ie. laravel, angular, bootstrap, etc)
id, name, order
levels (ie. none, beginner, intermediate, expert)
id, name, value
skills_has_levels
id, skills_id, levels_id, users_id
We have three models:
1) User (has many skills with levels)
2) Skill (has many levels)
3) Level (has many skills)
In the Skill model: (to have the users_id available: unused)
$this->belongsToMany('AppLevel’)
->withPivot(‘users_id’);
To list all users, their skills, and levels :
$users = User::all()->toArray();
foreach($users as $user)
echo "User ID: ".$user->id;
$skills = Skill::where(‘users_id’, ’=‘, $user->id)->get()->toArray();
print_r($skills);
echo "nnn";
Ultimately, I would want the User model to return a skills/levels array of all the associated user skills & their levels: skill name, skill order, level name and level value.
What I currently do works but i feel it could be done better.
whats the best way to structure the relationships for the User model to connect to skills names and the associated levels values?
Is it possible to related the Skill/Level directly to the User model? So i only have to use the User model to access all of the associated skill and levels.
Or are my relationships wrong? Could change the models to this relationship:
A User has many Skill
Skill has many skill types
Skill has many skill levels
Thanks in advance.
php laravel laravel-5 eloquent
I am trying to extend a User model with associated Skills and Levels. Where all users will have different skill levels for a variety of skills. Im trying to figure out the most efficient way to setup the relationships to accomplish this.
Im currently setting Skill to Level as a many to many relationship. User to skills_has_levels is one to many but of no current use.
There are many skills a user can have and each skill can have a different level from no experience to expert.
[Tables]
users
id, username, email, pwd, token, timestamps
skills (ie. laravel, angular, bootstrap, etc)
id, name, order
levels (ie. none, beginner, intermediate, expert)
id, name, value
skills_has_levels
id, skills_id, levels_id, users_id
We have three models:
1) User (has many skills with levels)
2) Skill (has many levels)
3) Level (has many skills)
In the Skill model: (to have the users_id available: unused)
$this->belongsToMany('AppLevel’)
->withPivot(‘users_id’);
To list all users, their skills, and levels :
$users = User::all()->toArray();
foreach($users as $user)
echo "User ID: ".$user->id;
$skills = Skill::where(‘users_id’, ’=‘, $user->id)->get()->toArray();
print_r($skills);
echo "nnn";
Ultimately, I would want the User model to return a skills/levels array of all the associated user skills & their levels: skill name, skill order, level name and level value.
What I currently do works but i feel it could be done better.
whats the best way to structure the relationships for the User model to connect to skills names and the associated levels values?
Is it possible to related the Skill/Level directly to the User model? So i only have to use the User model to access all of the associated skill and levels.
Or are my relationships wrong? Could change the models to this relationship:
A User has many Skill
Skill has many skill types
Skill has many skill levels
Thanks in advance.
php laravel laravel-5 eloquent
php laravel laravel-5 eloquent
edited Mar 26 at 13:46
Blitzo
asked Mar 25 at 16:33
BlitzoBlitzo
35 bronze badges
35 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your database makes the work harder i guess
You can use dynamic properties if you want to make it available in usermodel
Models User <- -(Many to many) -> Levels <- -(Many to many)
protected $appends = ['skills_levels'];
public function getSkillsLevelsAttribute()
$skills = // query to get all skills ->pluck('skill','id');
$this->load('levels.skills')
$levels = $this->levels->groupBy('skills.skill_id')->toarray();
$output = [];
foreach ($skills as $id => $skill)
$output[$skill] = [];
if (isset($levels[$id]))
$output[$skill] = $levels[$id] // all levels user have for this skill
return $output
Thanks. Normally I've seen other example where User to Skill is a many to many relationship and they put the skill level value (int) in the pivot table. Im trying to standardize the skill levels. Im trying to also minimize the query calls to show all of the skills / levels for a given user. How would you structure the database instead?
– Blitzo
Mar 26 at 15:07
how would you recommend to make it more efficient?
– Blitzo
Mar 26 at 15:11
it should be , User -(hasMany)> Skills -(hasMany)> Levels (Both are many to many so you will have two pivot tables but it's easy to manage with laravel), then usewith('skills.levels')
in ur query which won't result in many calls anyway after that, you can use dynamic properties as mentioned in my answer to map/format the data as you like or if you want it to happen automatically without the with just add$this->load('skills.levels')
at start ofgetSkillsLevelsAttribute()
method
– Achraf Khouadja
Mar 26 at 16:00
the skills are set and so are the possible levels. how do i connect the skills to their levels? is skills.levels another pivot table similar to my skills_has_levels?
– Blitzo
Mar 27 at 12:10
1
actually, i will just do what you recommended by the opposite. i will store the level_id in the Skills pivot and then just query the Levels to create a value key pair for all user skills and levels. not as nice as i wanted but will do the trick.
– Blitzo
Mar 27 at 15:34
|
show 7 more comments
Well there are few changes to be done on the table skills_has_levels
you should change
skills_id
to shareable_skills_id
levels_id
to shareable_levels_id
and finally update your classes like below
Class SkillsHasLevels
public function shareable()
return $this->morphTo();
Class Skill
public function shares()
return $this->morphOne(SkillsHasLevels::class, 'shareable');
Class User
public function skills()
return $this->morphedByMany(Skill::class, 'shareable', 'shares');
I hope this will return you the skills of the user
Thank you for your help. However, your solution doesnt help me. I already get something similar with just a many to many relationship between skills and levels. What I want is for Skill and Level to be accessible through the User model.
– Blitzo
Mar 26 at 10:49
Skill has a many to many relationship to Level. I want the User model to connect to this Skill/Level relationship. With your polymorphic relationship, I can only get what skills a user has but not the level of those skills. It also shows the levels but not how the levels connect to which skills.
– Blitzo
Mar 26 at 13:42
Ok, in addition add the below methods on your modelsSkill
andLevel
onSkill
modelpublic function levels() return $this->shares->levels;
and onLevel
modelpublic function skills() return $this->shares->skills;
and i'm not sure this will work, you gotta try it
– Sethu
Mar 26 at 14:07
yes, this is what i tried. it returns an array of the levels or an array of the skills but not of how they link. I would like it to return 'skill_1' => 'expert', 'skill_2' => 'beginner', etc
– Blitzo
Mar 26 at 15:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342447%2fhow-to-extend-a-users-model-with-another-many-to-many-relationship-models-skills%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your database makes the work harder i guess
You can use dynamic properties if you want to make it available in usermodel
Models User <- -(Many to many) -> Levels <- -(Many to many)
protected $appends = ['skills_levels'];
public function getSkillsLevelsAttribute()
$skills = // query to get all skills ->pluck('skill','id');
$this->load('levels.skills')
$levels = $this->levels->groupBy('skills.skill_id')->toarray();
$output = [];
foreach ($skills as $id => $skill)
$output[$skill] = [];
if (isset($levels[$id]))
$output[$skill] = $levels[$id] // all levels user have for this skill
return $output
Thanks. Normally I've seen other example where User to Skill is a many to many relationship and they put the skill level value (int) in the pivot table. Im trying to standardize the skill levels. Im trying to also minimize the query calls to show all of the skills / levels for a given user. How would you structure the database instead?
– Blitzo
Mar 26 at 15:07
how would you recommend to make it more efficient?
– Blitzo
Mar 26 at 15:11
it should be , User -(hasMany)> Skills -(hasMany)> Levels (Both are many to many so you will have two pivot tables but it's easy to manage with laravel), then usewith('skills.levels')
in ur query which won't result in many calls anyway after that, you can use dynamic properties as mentioned in my answer to map/format the data as you like or if you want it to happen automatically without the with just add$this->load('skills.levels')
at start ofgetSkillsLevelsAttribute()
method
– Achraf Khouadja
Mar 26 at 16:00
the skills are set and so are the possible levels. how do i connect the skills to their levels? is skills.levels another pivot table similar to my skills_has_levels?
– Blitzo
Mar 27 at 12:10
1
actually, i will just do what you recommended by the opposite. i will store the level_id in the Skills pivot and then just query the Levels to create a value key pair for all user skills and levels. not as nice as i wanted but will do the trick.
– Blitzo
Mar 27 at 15:34
|
show 7 more comments
Your database makes the work harder i guess
You can use dynamic properties if you want to make it available in usermodel
Models User <- -(Many to many) -> Levels <- -(Many to many)
protected $appends = ['skills_levels'];
public function getSkillsLevelsAttribute()
$skills = // query to get all skills ->pluck('skill','id');
$this->load('levels.skills')
$levels = $this->levels->groupBy('skills.skill_id')->toarray();
$output = [];
foreach ($skills as $id => $skill)
$output[$skill] = [];
if (isset($levels[$id]))
$output[$skill] = $levels[$id] // all levels user have for this skill
return $output
Thanks. Normally I've seen other example where User to Skill is a many to many relationship and they put the skill level value (int) in the pivot table. Im trying to standardize the skill levels. Im trying to also minimize the query calls to show all of the skills / levels for a given user. How would you structure the database instead?
– Blitzo
Mar 26 at 15:07
how would you recommend to make it more efficient?
– Blitzo
Mar 26 at 15:11
it should be , User -(hasMany)> Skills -(hasMany)> Levels (Both are many to many so you will have two pivot tables but it's easy to manage with laravel), then usewith('skills.levels')
in ur query which won't result in many calls anyway after that, you can use dynamic properties as mentioned in my answer to map/format the data as you like or if you want it to happen automatically without the with just add$this->load('skills.levels')
at start ofgetSkillsLevelsAttribute()
method
– Achraf Khouadja
Mar 26 at 16:00
the skills are set and so are the possible levels. how do i connect the skills to their levels? is skills.levels another pivot table similar to my skills_has_levels?
– Blitzo
Mar 27 at 12:10
1
actually, i will just do what you recommended by the opposite. i will store the level_id in the Skills pivot and then just query the Levels to create a value key pair for all user skills and levels. not as nice as i wanted but will do the trick.
– Blitzo
Mar 27 at 15:34
|
show 7 more comments
Your database makes the work harder i guess
You can use dynamic properties if you want to make it available in usermodel
Models User <- -(Many to many) -> Levels <- -(Many to many)
protected $appends = ['skills_levels'];
public function getSkillsLevelsAttribute()
$skills = // query to get all skills ->pluck('skill','id');
$this->load('levels.skills')
$levels = $this->levels->groupBy('skills.skill_id')->toarray();
$output = [];
foreach ($skills as $id => $skill)
$output[$skill] = [];
if (isset($levels[$id]))
$output[$skill] = $levels[$id] // all levels user have for this skill
return $output
Your database makes the work harder i guess
You can use dynamic properties if you want to make it available in usermodel
Models User <- -(Many to many) -> Levels <- -(Many to many)
protected $appends = ['skills_levels'];
public function getSkillsLevelsAttribute()
$skills = // query to get all skills ->pluck('skill','id');
$this->load('levels.skills')
$levels = $this->levels->groupBy('skills.skill_id')->toarray();
$output = [];
foreach ($skills as $id => $skill)
$output[$skill] = [];
if (isset($levels[$id]))
$output[$skill] = $levels[$id] // all levels user have for this skill
return $output
edited Mar 27 at 14:15
answered Mar 26 at 14:12
Achraf KhouadjaAchraf Khouadja
3,9392 gold badges16 silver badges32 bronze badges
3,9392 gold badges16 silver badges32 bronze badges
Thanks. Normally I've seen other example where User to Skill is a many to many relationship and they put the skill level value (int) in the pivot table. Im trying to standardize the skill levels. Im trying to also minimize the query calls to show all of the skills / levels for a given user. How would you structure the database instead?
– Blitzo
Mar 26 at 15:07
how would you recommend to make it more efficient?
– Blitzo
Mar 26 at 15:11
it should be , User -(hasMany)> Skills -(hasMany)> Levels (Both are many to many so you will have two pivot tables but it's easy to manage with laravel), then usewith('skills.levels')
in ur query which won't result in many calls anyway after that, you can use dynamic properties as mentioned in my answer to map/format the data as you like or if you want it to happen automatically without the with just add$this->load('skills.levels')
at start ofgetSkillsLevelsAttribute()
method
– Achraf Khouadja
Mar 26 at 16:00
the skills are set and so are the possible levels. how do i connect the skills to their levels? is skills.levels another pivot table similar to my skills_has_levels?
– Blitzo
Mar 27 at 12:10
1
actually, i will just do what you recommended by the opposite. i will store the level_id in the Skills pivot and then just query the Levels to create a value key pair for all user skills and levels. not as nice as i wanted but will do the trick.
– Blitzo
Mar 27 at 15:34
|
show 7 more comments
Thanks. Normally I've seen other example where User to Skill is a many to many relationship and they put the skill level value (int) in the pivot table. Im trying to standardize the skill levels. Im trying to also minimize the query calls to show all of the skills / levels for a given user. How would you structure the database instead?
– Blitzo
Mar 26 at 15:07
how would you recommend to make it more efficient?
– Blitzo
Mar 26 at 15:11
it should be , User -(hasMany)> Skills -(hasMany)> Levels (Both are many to many so you will have two pivot tables but it's easy to manage with laravel), then usewith('skills.levels')
in ur query which won't result in many calls anyway after that, you can use dynamic properties as mentioned in my answer to map/format the data as you like or if you want it to happen automatically without the with just add$this->load('skills.levels')
at start ofgetSkillsLevelsAttribute()
method
– Achraf Khouadja
Mar 26 at 16:00
the skills are set and so are the possible levels. how do i connect the skills to their levels? is skills.levels another pivot table similar to my skills_has_levels?
– Blitzo
Mar 27 at 12:10
1
actually, i will just do what you recommended by the opposite. i will store the level_id in the Skills pivot and then just query the Levels to create a value key pair for all user skills and levels. not as nice as i wanted but will do the trick.
– Blitzo
Mar 27 at 15:34
Thanks. Normally I've seen other example where User to Skill is a many to many relationship and they put the skill level value (int) in the pivot table. Im trying to standardize the skill levels. Im trying to also minimize the query calls to show all of the skills / levels for a given user. How would you structure the database instead?
– Blitzo
Mar 26 at 15:07
Thanks. Normally I've seen other example where User to Skill is a many to many relationship and they put the skill level value (int) in the pivot table. Im trying to standardize the skill levels. Im trying to also minimize the query calls to show all of the skills / levels for a given user. How would you structure the database instead?
– Blitzo
Mar 26 at 15:07
how would you recommend to make it more efficient?
– Blitzo
Mar 26 at 15:11
how would you recommend to make it more efficient?
– Blitzo
Mar 26 at 15:11
it should be , User -(hasMany)> Skills -(hasMany)> Levels (Both are many to many so you will have two pivot tables but it's easy to manage with laravel), then use
with('skills.levels')
in ur query which won't result in many calls anyway after that, you can use dynamic properties as mentioned in my answer to map/format the data as you like or if you want it to happen automatically without the with just add $this->load('skills.levels')
at start of getSkillsLevelsAttribute()
method– Achraf Khouadja
Mar 26 at 16:00
it should be , User -(hasMany)> Skills -(hasMany)> Levels (Both are many to many so you will have two pivot tables but it's easy to manage with laravel), then use
with('skills.levels')
in ur query which won't result in many calls anyway after that, you can use dynamic properties as mentioned in my answer to map/format the data as you like or if you want it to happen automatically without the with just add $this->load('skills.levels')
at start of getSkillsLevelsAttribute()
method– Achraf Khouadja
Mar 26 at 16:00
the skills are set and so are the possible levels. how do i connect the skills to their levels? is skills.levels another pivot table similar to my skills_has_levels?
– Blitzo
Mar 27 at 12:10
the skills are set and so are the possible levels. how do i connect the skills to their levels? is skills.levels another pivot table similar to my skills_has_levels?
– Blitzo
Mar 27 at 12:10
1
1
actually, i will just do what you recommended by the opposite. i will store the level_id in the Skills pivot and then just query the Levels to create a value key pair for all user skills and levels. not as nice as i wanted but will do the trick.
– Blitzo
Mar 27 at 15:34
actually, i will just do what you recommended by the opposite. i will store the level_id in the Skills pivot and then just query the Levels to create a value key pair for all user skills and levels. not as nice as i wanted but will do the trick.
– Blitzo
Mar 27 at 15:34
|
show 7 more comments
Well there are few changes to be done on the table skills_has_levels
you should change
skills_id
to shareable_skills_id
levels_id
to shareable_levels_id
and finally update your classes like below
Class SkillsHasLevels
public function shareable()
return $this->morphTo();
Class Skill
public function shares()
return $this->morphOne(SkillsHasLevels::class, 'shareable');
Class User
public function skills()
return $this->morphedByMany(Skill::class, 'shareable', 'shares');
I hope this will return you the skills of the user
Thank you for your help. However, your solution doesnt help me. I already get something similar with just a many to many relationship between skills and levels. What I want is for Skill and Level to be accessible through the User model.
– Blitzo
Mar 26 at 10:49
Skill has a many to many relationship to Level. I want the User model to connect to this Skill/Level relationship. With your polymorphic relationship, I can only get what skills a user has but not the level of those skills. It also shows the levels but not how the levels connect to which skills.
– Blitzo
Mar 26 at 13:42
Ok, in addition add the below methods on your modelsSkill
andLevel
onSkill
modelpublic function levels() return $this->shares->levels;
and onLevel
modelpublic function skills() return $this->shares->skills;
and i'm not sure this will work, you gotta try it
– Sethu
Mar 26 at 14:07
yes, this is what i tried. it returns an array of the levels or an array of the skills but not of how they link. I would like it to return 'skill_1' => 'expert', 'skill_2' => 'beginner', etc
– Blitzo
Mar 26 at 15:11
add a comment |
Well there are few changes to be done on the table skills_has_levels
you should change
skills_id
to shareable_skills_id
levels_id
to shareable_levels_id
and finally update your classes like below
Class SkillsHasLevels
public function shareable()
return $this->morphTo();
Class Skill
public function shares()
return $this->morphOne(SkillsHasLevels::class, 'shareable');
Class User
public function skills()
return $this->morphedByMany(Skill::class, 'shareable', 'shares');
I hope this will return you the skills of the user
Thank you for your help. However, your solution doesnt help me. I already get something similar with just a many to many relationship between skills and levels. What I want is for Skill and Level to be accessible through the User model.
– Blitzo
Mar 26 at 10:49
Skill has a many to many relationship to Level. I want the User model to connect to this Skill/Level relationship. With your polymorphic relationship, I can only get what skills a user has but not the level of those skills. It also shows the levels but not how the levels connect to which skills.
– Blitzo
Mar 26 at 13:42
Ok, in addition add the below methods on your modelsSkill
andLevel
onSkill
modelpublic function levels() return $this->shares->levels;
and onLevel
modelpublic function skills() return $this->shares->skills;
and i'm not sure this will work, you gotta try it
– Sethu
Mar 26 at 14:07
yes, this is what i tried. it returns an array of the levels or an array of the skills but not of how they link. I would like it to return 'skill_1' => 'expert', 'skill_2' => 'beginner', etc
– Blitzo
Mar 26 at 15:11
add a comment |
Well there are few changes to be done on the table skills_has_levels
you should change
skills_id
to shareable_skills_id
levels_id
to shareable_levels_id
and finally update your classes like below
Class SkillsHasLevels
public function shareable()
return $this->morphTo();
Class Skill
public function shares()
return $this->morphOne(SkillsHasLevels::class, 'shareable');
Class User
public function skills()
return $this->morphedByMany(Skill::class, 'shareable', 'shares');
I hope this will return you the skills of the user
Well there are few changes to be done on the table skills_has_levels
you should change
skills_id
to shareable_skills_id
levels_id
to shareable_levels_id
and finally update your classes like below
Class SkillsHasLevels
public function shareable()
return $this->morphTo();
Class Skill
public function shares()
return $this->morphOne(SkillsHasLevels::class, 'shareable');
Class User
public function skills()
return $this->morphedByMany(Skill::class, 'shareable', 'shares');
I hope this will return you the skills of the user
answered Mar 25 at 17:14
SethuSethu
6704 silver badges8 bronze badges
6704 silver badges8 bronze badges
Thank you for your help. However, your solution doesnt help me. I already get something similar with just a many to many relationship between skills and levels. What I want is for Skill and Level to be accessible through the User model.
– Blitzo
Mar 26 at 10:49
Skill has a many to many relationship to Level. I want the User model to connect to this Skill/Level relationship. With your polymorphic relationship, I can only get what skills a user has but not the level of those skills. It also shows the levels but not how the levels connect to which skills.
– Blitzo
Mar 26 at 13:42
Ok, in addition add the below methods on your modelsSkill
andLevel
onSkill
modelpublic function levels() return $this->shares->levels;
and onLevel
modelpublic function skills() return $this->shares->skills;
and i'm not sure this will work, you gotta try it
– Sethu
Mar 26 at 14:07
yes, this is what i tried. it returns an array of the levels or an array of the skills but not of how they link. I would like it to return 'skill_1' => 'expert', 'skill_2' => 'beginner', etc
– Blitzo
Mar 26 at 15:11
add a comment |
Thank you for your help. However, your solution doesnt help me. I already get something similar with just a many to many relationship between skills and levels. What I want is for Skill and Level to be accessible through the User model.
– Blitzo
Mar 26 at 10:49
Skill has a many to many relationship to Level. I want the User model to connect to this Skill/Level relationship. With your polymorphic relationship, I can only get what skills a user has but not the level of those skills. It also shows the levels but not how the levels connect to which skills.
– Blitzo
Mar 26 at 13:42
Ok, in addition add the below methods on your modelsSkill
andLevel
onSkill
modelpublic function levels() return $this->shares->levels;
and onLevel
modelpublic function skills() return $this->shares->skills;
and i'm not sure this will work, you gotta try it
– Sethu
Mar 26 at 14:07
yes, this is what i tried. it returns an array of the levels or an array of the skills but not of how they link. I would like it to return 'skill_1' => 'expert', 'skill_2' => 'beginner', etc
– Blitzo
Mar 26 at 15:11
Thank you for your help. However, your solution doesnt help me. I already get something similar with just a many to many relationship between skills and levels. What I want is for Skill and Level to be accessible through the User model.
– Blitzo
Mar 26 at 10:49
Thank you for your help. However, your solution doesnt help me. I already get something similar with just a many to many relationship between skills and levels. What I want is for Skill and Level to be accessible through the User model.
– Blitzo
Mar 26 at 10:49
Skill has a many to many relationship to Level. I want the User model to connect to this Skill/Level relationship. With your polymorphic relationship, I can only get what skills a user has but not the level of those skills. It also shows the levels but not how the levels connect to which skills.
– Blitzo
Mar 26 at 13:42
Skill has a many to many relationship to Level. I want the User model to connect to this Skill/Level relationship. With your polymorphic relationship, I can only get what skills a user has but not the level of those skills. It also shows the levels but not how the levels connect to which skills.
– Blitzo
Mar 26 at 13:42
Ok, in addition add the below methods on your models
Skill
and Level
on Skill
model public function levels() return $this->shares->levels;
and on Level
model public function skills() return $this->shares->skills;
and i'm not sure this will work, you gotta try it– Sethu
Mar 26 at 14:07
Ok, in addition add the below methods on your models
Skill
and Level
on Skill
model public function levels() return $this->shares->levels;
and on Level
model public function skills() return $this->shares->skills;
and i'm not sure this will work, you gotta try it– Sethu
Mar 26 at 14:07
yes, this is what i tried. it returns an array of the levels or an array of the skills but not of how they link. I would like it to return 'skill_1' => 'expert', 'skill_2' => 'beginner', etc
– Blitzo
Mar 26 at 15:11
yes, this is what i tried. it returns an array of the levels or an array of the skills but not of how they link. I would like it to return 'skill_1' => 'expert', 'skill_2' => 'beginner', etc
– Blitzo
Mar 26 at 15:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342447%2fhow-to-extend-a-users-model-with-another-many-to-many-relationship-models-skills%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown