Right relation for eloquent model
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
After several attempts, I can not make an eloquent "relations" work.
I created a database with MySQL Workbech which contains the following tables:
- Actors
- ActorData
- Nationality
Their relationship image:
In PHP instead I have the following code (summary):
class ActorData extends Model
protected $table = 'actordata';
[..]
public function nationality()
return $this->hasOne("AppModelsActorDataNationality");
class Actor extends Model
protected $table = 'actors';
[..]
public function actorData()
return $this->hasOne('AppModelsActorData');
My goal is, when i call "->nationality", i wish it takes the "nationality", but i recive "null" or errors about "keys" not found.
$actorData = AppModelsActor::find(1)->actorData()->first();
var_dump($actorData->nationality);
I understand that "hasOne" looks for the key in "nationality", but I wanted to make a simple "link".
As another solution, so I could simply take the "Nationality_Id" and look for it in the "Nationality" table, but I think it's a wrong approach.
I hope I have been exhaustive, thanks for your cooperation.
( in order to "fix" i had to add "$actorData->nationality()->associate($nationality))
php eloquent relational-database mysql-workbench slim-3
add a comment |
After several attempts, I can not make an eloquent "relations" work.
I created a database with MySQL Workbech which contains the following tables:
- Actors
- ActorData
- Nationality
Their relationship image:
In PHP instead I have the following code (summary):
class ActorData extends Model
protected $table = 'actordata';
[..]
public function nationality()
return $this->hasOne("AppModelsActorDataNationality");
class Actor extends Model
protected $table = 'actors';
[..]
public function actorData()
return $this->hasOne('AppModelsActorData');
My goal is, when i call "->nationality", i wish it takes the "nationality", but i recive "null" or errors about "keys" not found.
$actorData = AppModelsActor::find(1)->actorData()->first();
var_dump($actorData->nationality);
I understand that "hasOne" looks for the key in "nationality", but I wanted to make a simple "link".
As another solution, so I could simply take the "Nationality_Id" and look for it in the "Nationality" table, but I think it's a wrong approach.
I hope I have been exhaustive, thanks for your cooperation.
( in order to "fix" i had to add "$actorData->nationality()->associate($nationality))
php eloquent relational-database mysql-workbench slim-3
add a comment |
After several attempts, I can not make an eloquent "relations" work.
I created a database with MySQL Workbech which contains the following tables:
- Actors
- ActorData
- Nationality
Their relationship image:
In PHP instead I have the following code (summary):
class ActorData extends Model
protected $table = 'actordata';
[..]
public function nationality()
return $this->hasOne("AppModelsActorDataNationality");
class Actor extends Model
protected $table = 'actors';
[..]
public function actorData()
return $this->hasOne('AppModelsActorData');
My goal is, when i call "->nationality", i wish it takes the "nationality", but i recive "null" or errors about "keys" not found.
$actorData = AppModelsActor::find(1)->actorData()->first();
var_dump($actorData->nationality);
I understand that "hasOne" looks for the key in "nationality", but I wanted to make a simple "link".
As another solution, so I could simply take the "Nationality_Id" and look for it in the "Nationality" table, but I think it's a wrong approach.
I hope I have been exhaustive, thanks for your cooperation.
( in order to "fix" i had to add "$actorData->nationality()->associate($nationality))
php eloquent relational-database mysql-workbench slim-3
After several attempts, I can not make an eloquent "relations" work.
I created a database with MySQL Workbech which contains the following tables:
- Actors
- ActorData
- Nationality
Their relationship image:
In PHP instead I have the following code (summary):
class ActorData extends Model
protected $table = 'actordata';
[..]
public function nationality()
return $this->hasOne("AppModelsActorDataNationality");
class Actor extends Model
protected $table = 'actors';
[..]
public function actorData()
return $this->hasOne('AppModelsActorData');
My goal is, when i call "->nationality", i wish it takes the "nationality", but i recive "null" or errors about "keys" not found.
$actorData = AppModelsActor::find(1)->actorData()->first();
var_dump($actorData->nationality);
I understand that "hasOne" looks for the key in "nationality", but I wanted to make a simple "link".
As another solution, so I could simply take the "Nationality_Id" and look for it in the "Nationality" table, but I think it's a wrong approach.
I hope I have been exhaustive, thanks for your cooperation.
( in order to "fix" i had to add "$actorData->nationality()->associate($nationality))
php eloquent relational-database mysql-workbench slim-3
php eloquent relational-database mysql-workbench slim-3
edited Nov 19 '18 at 10:30
Andrea Bori
asked Nov 15 '18 at 10:14
Andrea BoriAndrea Bori
173113
173113
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You relationship should look something like the above.
Where actorData is a pivot table for the 2 other tables.
Your classes should look like this:
class Actors extends Model
public function nationality()
return $this->belongsToMany(Nationality::class);
class Nationality extends Model
public function actors()
return $this->belongsToMany(Actors::class);
You're basically looking at Many-to-Many. Where each actor can have many nationalities while at the same time each nationality can belong to many actors.
Normally laravel expects the pivot table to be named something like actor_nationality
, but you can specify the table name:
return $this->belongsToMany(ModelClassGoesHere, 'PIVOT TABLE GOES HERE', 'FIRST TABLE ID', 'SECOND TABLE ID');
ow, i understand but. This means that somehow actor is a "child" of nationality? tomorrow i'll try ur version :)
– Andrea Bori
Nov 15 '18 at 14:43
Is a many-to-many definitely correct? A nationality may have many actors, but an actor should only have 1 nationality.
– John Halsey
Nov 15 '18 at 15:40
@JohnHalsey it should be something like "an actor have a nationality" only one, so nationality i don't know how it should be "related" :(
– Andrea Bori
Nov 15 '18 at 15:42
Well I mean people can have more than one nationality. But redefine it as you see fit.
– Andrei
Nov 16 '18 at 5:42
each record of nationality is a "nationality", like at 1 we have "English", the it_IT means "Inglese" then the column en_US means "English", then i need to connect only one nationality to each actor, so when i call "$actor->nationality" i receave "1" if is english. i can achieve this using other methos, put i suppose is a best practices using relational database.. :(
– Andrea Bori
Nov 17 '18 at 11:00
|
show 1 more comment
Why can't you put the nationality_id
directly on the actors
table since, they can only have one. Then your relationship on the Actor
would be
public function nationality()
return $this->hasOne(Nationality::class);
Then you could do $actor->nationality;
Your nationality class would be...
class Nationality extends Model
public function actors()
return $this->hasMany(Actor::class);
yeah i know, that's is what i suppose to do, but only errors. It's like eloquent, expect to do other things. It has a strange behaviour.. but that way doesn't work. Dunno why
– Andrea Bori
Nov 19 '18 at 8:48
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%2f53317076%2fright-relation-for-eloquent-model%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
You relationship should look something like the above.
Where actorData is a pivot table for the 2 other tables.
Your classes should look like this:
class Actors extends Model
public function nationality()
return $this->belongsToMany(Nationality::class);
class Nationality extends Model
public function actors()
return $this->belongsToMany(Actors::class);
You're basically looking at Many-to-Many. Where each actor can have many nationalities while at the same time each nationality can belong to many actors.
Normally laravel expects the pivot table to be named something like actor_nationality
, but you can specify the table name:
return $this->belongsToMany(ModelClassGoesHere, 'PIVOT TABLE GOES HERE', 'FIRST TABLE ID', 'SECOND TABLE ID');
ow, i understand but. This means that somehow actor is a "child" of nationality? tomorrow i'll try ur version :)
– Andrea Bori
Nov 15 '18 at 14:43
Is a many-to-many definitely correct? A nationality may have many actors, but an actor should only have 1 nationality.
– John Halsey
Nov 15 '18 at 15:40
@JohnHalsey it should be something like "an actor have a nationality" only one, so nationality i don't know how it should be "related" :(
– Andrea Bori
Nov 15 '18 at 15:42
Well I mean people can have more than one nationality. But redefine it as you see fit.
– Andrei
Nov 16 '18 at 5:42
each record of nationality is a "nationality", like at 1 we have "English", the it_IT means "Inglese" then the column en_US means "English", then i need to connect only one nationality to each actor, so when i call "$actor->nationality" i receave "1" if is english. i can achieve this using other methos, put i suppose is a best practices using relational database.. :(
– Andrea Bori
Nov 17 '18 at 11:00
|
show 1 more comment
You relationship should look something like the above.
Where actorData is a pivot table for the 2 other tables.
Your classes should look like this:
class Actors extends Model
public function nationality()
return $this->belongsToMany(Nationality::class);
class Nationality extends Model
public function actors()
return $this->belongsToMany(Actors::class);
You're basically looking at Many-to-Many. Where each actor can have many nationalities while at the same time each nationality can belong to many actors.
Normally laravel expects the pivot table to be named something like actor_nationality
, but you can specify the table name:
return $this->belongsToMany(ModelClassGoesHere, 'PIVOT TABLE GOES HERE', 'FIRST TABLE ID', 'SECOND TABLE ID');
ow, i understand but. This means that somehow actor is a "child" of nationality? tomorrow i'll try ur version :)
– Andrea Bori
Nov 15 '18 at 14:43
Is a many-to-many definitely correct? A nationality may have many actors, but an actor should only have 1 nationality.
– John Halsey
Nov 15 '18 at 15:40
@JohnHalsey it should be something like "an actor have a nationality" only one, so nationality i don't know how it should be "related" :(
– Andrea Bori
Nov 15 '18 at 15:42
Well I mean people can have more than one nationality. But redefine it as you see fit.
– Andrei
Nov 16 '18 at 5:42
each record of nationality is a "nationality", like at 1 we have "English", the it_IT means "Inglese" then the column en_US means "English", then i need to connect only one nationality to each actor, so when i call "$actor->nationality" i receave "1" if is english. i can achieve this using other methos, put i suppose is a best practices using relational database.. :(
– Andrea Bori
Nov 17 '18 at 11:00
|
show 1 more comment
You relationship should look something like the above.
Where actorData is a pivot table for the 2 other tables.
Your classes should look like this:
class Actors extends Model
public function nationality()
return $this->belongsToMany(Nationality::class);
class Nationality extends Model
public function actors()
return $this->belongsToMany(Actors::class);
You're basically looking at Many-to-Many. Where each actor can have many nationalities while at the same time each nationality can belong to many actors.
Normally laravel expects the pivot table to be named something like actor_nationality
, but you can specify the table name:
return $this->belongsToMany(ModelClassGoesHere, 'PIVOT TABLE GOES HERE', 'FIRST TABLE ID', 'SECOND TABLE ID');
You relationship should look something like the above.
Where actorData is a pivot table for the 2 other tables.
Your classes should look like this:
class Actors extends Model
public function nationality()
return $this->belongsToMany(Nationality::class);
class Nationality extends Model
public function actors()
return $this->belongsToMany(Actors::class);
You're basically looking at Many-to-Many. Where each actor can have many nationalities while at the same time each nationality can belong to many actors.
Normally laravel expects the pivot table to be named something like actor_nationality
, but you can specify the table name:
return $this->belongsToMany(ModelClassGoesHere, 'PIVOT TABLE GOES HERE', 'FIRST TABLE ID', 'SECOND TABLE ID');
answered Nov 15 '18 at 10:32
AndreiAndrei
2,07031323
2,07031323
ow, i understand but. This means that somehow actor is a "child" of nationality? tomorrow i'll try ur version :)
– Andrea Bori
Nov 15 '18 at 14:43
Is a many-to-many definitely correct? A nationality may have many actors, but an actor should only have 1 nationality.
– John Halsey
Nov 15 '18 at 15:40
@JohnHalsey it should be something like "an actor have a nationality" only one, so nationality i don't know how it should be "related" :(
– Andrea Bori
Nov 15 '18 at 15:42
Well I mean people can have more than one nationality. But redefine it as you see fit.
– Andrei
Nov 16 '18 at 5:42
each record of nationality is a "nationality", like at 1 we have "English", the it_IT means "Inglese" then the column en_US means "English", then i need to connect only one nationality to each actor, so when i call "$actor->nationality" i receave "1" if is english. i can achieve this using other methos, put i suppose is a best practices using relational database.. :(
– Andrea Bori
Nov 17 '18 at 11:00
|
show 1 more comment
ow, i understand but. This means that somehow actor is a "child" of nationality? tomorrow i'll try ur version :)
– Andrea Bori
Nov 15 '18 at 14:43
Is a many-to-many definitely correct? A nationality may have many actors, but an actor should only have 1 nationality.
– John Halsey
Nov 15 '18 at 15:40
@JohnHalsey it should be something like "an actor have a nationality" only one, so nationality i don't know how it should be "related" :(
– Andrea Bori
Nov 15 '18 at 15:42
Well I mean people can have more than one nationality. But redefine it as you see fit.
– Andrei
Nov 16 '18 at 5:42
each record of nationality is a "nationality", like at 1 we have "English", the it_IT means "Inglese" then the column en_US means "English", then i need to connect only one nationality to each actor, so when i call "$actor->nationality" i receave "1" if is english. i can achieve this using other methos, put i suppose is a best practices using relational database.. :(
– Andrea Bori
Nov 17 '18 at 11:00
ow, i understand but. This means that somehow actor is a "child" of nationality? tomorrow i'll try ur version :)
– Andrea Bori
Nov 15 '18 at 14:43
ow, i understand but. This means that somehow actor is a "child" of nationality? tomorrow i'll try ur version :)
– Andrea Bori
Nov 15 '18 at 14:43
Is a many-to-many definitely correct? A nationality may have many actors, but an actor should only have 1 nationality.
– John Halsey
Nov 15 '18 at 15:40
Is a many-to-many definitely correct? A nationality may have many actors, but an actor should only have 1 nationality.
– John Halsey
Nov 15 '18 at 15:40
@JohnHalsey it should be something like "an actor have a nationality" only one, so nationality i don't know how it should be "related" :(
– Andrea Bori
Nov 15 '18 at 15:42
@JohnHalsey it should be something like "an actor have a nationality" only one, so nationality i don't know how it should be "related" :(
– Andrea Bori
Nov 15 '18 at 15:42
Well I mean people can have more than one nationality. But redefine it as you see fit.
– Andrei
Nov 16 '18 at 5:42
Well I mean people can have more than one nationality. But redefine it as you see fit.
– Andrei
Nov 16 '18 at 5:42
each record of nationality is a "nationality", like at 1 we have "English", the it_IT means "Inglese" then the column en_US means "English", then i need to connect only one nationality to each actor, so when i call "$actor->nationality" i receave "1" if is english. i can achieve this using other methos, put i suppose is a best practices using relational database.. :(
– Andrea Bori
Nov 17 '18 at 11:00
each record of nationality is a "nationality", like at 1 we have "English", the it_IT means "Inglese" then the column en_US means "English", then i need to connect only one nationality to each actor, so when i call "$actor->nationality" i receave "1" if is english. i can achieve this using other methos, put i suppose is a best practices using relational database.. :(
– Andrea Bori
Nov 17 '18 at 11:00
|
show 1 more comment
Why can't you put the nationality_id
directly on the actors
table since, they can only have one. Then your relationship on the Actor
would be
public function nationality()
return $this->hasOne(Nationality::class);
Then you could do $actor->nationality;
Your nationality class would be...
class Nationality extends Model
public function actors()
return $this->hasMany(Actor::class);
yeah i know, that's is what i suppose to do, but only errors. It's like eloquent, expect to do other things. It has a strange behaviour.. but that way doesn't work. Dunno why
– Andrea Bori
Nov 19 '18 at 8:48
add a comment |
Why can't you put the nationality_id
directly on the actors
table since, they can only have one. Then your relationship on the Actor
would be
public function nationality()
return $this->hasOne(Nationality::class);
Then you could do $actor->nationality;
Your nationality class would be...
class Nationality extends Model
public function actors()
return $this->hasMany(Actor::class);
yeah i know, that's is what i suppose to do, but only errors. It's like eloquent, expect to do other things. It has a strange behaviour.. but that way doesn't work. Dunno why
– Andrea Bori
Nov 19 '18 at 8:48
add a comment |
Why can't you put the nationality_id
directly on the actors
table since, they can only have one. Then your relationship on the Actor
would be
public function nationality()
return $this->hasOne(Nationality::class);
Then you could do $actor->nationality;
Your nationality class would be...
class Nationality extends Model
public function actors()
return $this->hasMany(Actor::class);
Why can't you put the nationality_id
directly on the actors
table since, they can only have one. Then your relationship on the Actor
would be
public function nationality()
return $this->hasOne(Nationality::class);
Then you could do $actor->nationality;
Your nationality class would be...
class Nationality extends Model
public function actors()
return $this->hasMany(Actor::class);
answered Nov 15 '18 at 15:49
John HalseyJohn Halsey
923825
923825
yeah i know, that's is what i suppose to do, but only errors. It's like eloquent, expect to do other things. It has a strange behaviour.. but that way doesn't work. Dunno why
– Andrea Bori
Nov 19 '18 at 8:48
add a comment |
yeah i know, that's is what i suppose to do, but only errors. It's like eloquent, expect to do other things. It has a strange behaviour.. but that way doesn't work. Dunno why
– Andrea Bori
Nov 19 '18 at 8:48
yeah i know, that's is what i suppose to do, but only errors. It's like eloquent, expect to do other things. It has a strange behaviour.. but that way doesn't work. Dunno why
– Andrea Bori
Nov 19 '18 at 8:48
yeah i know, that's is what i suppose to do, but only errors. It's like eloquent, expect to do other things. It has a strange behaviour.. but that way doesn't work. Dunno why
– Andrea Bori
Nov 19 '18 at 8:48
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%2f53317076%2fright-relation-for-eloquent-model%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