hasOne or BelongsTo in Laravel?
I have two tables: Photos
and ProductPhotos
where ProductPhotos
is:
ProductPhotos
_______________
product_id | photo_id
And Photos
is:
Photos
_______________
id | photo
Does it mean that relation beetween ProductPhotos -> Photos
as belongsTo
or hasOne
yet?
Cause photo_id
is foreign key, I guess it is belogsTo
laravel laravel-5
add a comment |
I have two tables: Photos
and ProductPhotos
where ProductPhotos
is:
ProductPhotos
_______________
product_id | photo_id
And Photos
is:
Photos
_______________
id | photo
Does it mean that relation beetween ProductPhotos -> Photos
as belongsTo
or hasOne
yet?
Cause photo_id
is foreign key, I guess it is belogsTo
laravel laravel-5
add a comment |
I have two tables: Photos
and ProductPhotos
where ProductPhotos
is:
ProductPhotos
_______________
product_id | photo_id
And Photos
is:
Photos
_______________
id | photo
Does it mean that relation beetween ProductPhotos -> Photos
as belongsTo
or hasOne
yet?
Cause photo_id
is foreign key, I guess it is belogsTo
laravel laravel-5
I have two tables: Photos
and ProductPhotos
where ProductPhotos
is:
ProductPhotos
_______________
product_id | photo_id
And Photos
is:
Photos
_______________
id | photo
Does it mean that relation beetween ProductPhotos -> Photos
as belongsTo
or hasOne
yet?
Cause photo_id
is foreign key, I guess it is belogsTo
laravel laravel-5
laravel laravel-5
asked Nov 15 '18 at 23:19
OPVOPV
1,71921542
1,71921542
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.
In your case, relations can be
- ProductPhotos belongsTo Products
- Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model
You can have detail understanding on relationship on laravel Docs Relationships
add a comment |
I would set up the relationships with
photos->hasOne->ProductPhotos->belongsTo->Products
Products->hasMany->ProductPhotos->belongsTo->Photos
This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
This will also depend on your use of the images of the products
Hope this helps
add a comment |
In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.
Use in both tables:
belongsToMany
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
I dont think so, cause I have intermediate table ProductPhoto that contains all photos links
– OPV
Nov 15 '18 at 23:38
So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table
– Tácio Brito
Nov 16 '18 at 12:28
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%2f53329232%2fhasone-or-belongsto-in-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.
In your case, relations can be
- ProductPhotos belongsTo Products
- Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model
You can have detail understanding on relationship on laravel Docs Relationships
add a comment |
A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.
In your case, relations can be
- ProductPhotos belongsTo Products
- Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model
You can have detail understanding on relationship on laravel Docs Relationships
add a comment |
A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.
In your case, relations can be
- ProductPhotos belongsTo Products
- Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model
You can have detail understanding on relationship on laravel Docs Relationships
A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.
In your case, relations can be
- ProductPhotos belongsTo Products
- Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model
You can have detail understanding on relationship on laravel Docs Relationships
answered Nov 16 '18 at 4:35
InvincibleElfInvincibleElf
9615
9615
add a comment |
add a comment |
I would set up the relationships with
photos->hasOne->ProductPhotos->belongsTo->Products
Products->hasMany->ProductPhotos->belongsTo->Photos
This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
This will also depend on your use of the images of the products
Hope this helps
add a comment |
I would set up the relationships with
photos->hasOne->ProductPhotos->belongsTo->Products
Products->hasMany->ProductPhotos->belongsTo->Photos
This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
This will also depend on your use of the images of the products
Hope this helps
add a comment |
I would set up the relationships with
photos->hasOne->ProductPhotos->belongsTo->Products
Products->hasMany->ProductPhotos->belongsTo->Photos
This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
This will also depend on your use of the images of the products
Hope this helps
I would set up the relationships with
photos->hasOne->ProductPhotos->belongsTo->Products
Products->hasMany->ProductPhotos->belongsTo->Photos
This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
This will also depend on your use of the images of the products
Hope this helps
answered Nov 15 '18 at 23:34
JoshJosh
718317
718317
add a comment |
add a comment |
In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.
Use in both tables:
belongsToMany
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
I dont think so, cause I have intermediate table ProductPhoto that contains all photos links
– OPV
Nov 15 '18 at 23:38
So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table
– Tácio Brito
Nov 16 '18 at 12:28
add a comment |
In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.
Use in both tables:
belongsToMany
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
I dont think so, cause I have intermediate table ProductPhoto that contains all photos links
– OPV
Nov 15 '18 at 23:38
So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table
– Tácio Brito
Nov 16 '18 at 12:28
add a comment |
In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.
Use in both tables:
belongsToMany
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.
Use in both tables:
belongsToMany
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
answered Nov 15 '18 at 23:33
Tácio BritoTácio Brito
11
11
I dont think so, cause I have intermediate table ProductPhoto that contains all photos links
– OPV
Nov 15 '18 at 23:38
So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table
– Tácio Brito
Nov 16 '18 at 12:28
add a comment |
I dont think so, cause I have intermediate table ProductPhoto that contains all photos links
– OPV
Nov 15 '18 at 23:38
So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table
– Tácio Brito
Nov 16 '18 at 12:28
I dont think so, cause I have intermediate table ProductPhoto that contains all photos links
– OPV
Nov 15 '18 at 23:38
I dont think so, cause I have intermediate table ProductPhoto that contains all photos links
– OPV
Nov 15 '18 at 23:38
So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table
– Tácio Brito
Nov 16 '18 at 12:28
So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table
– Tácio Brito
Nov 16 '18 at 12:28
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%2f53329232%2fhasone-or-belongsto-in-laravel%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