Laravel 5.6 OneToOne relation not working
I have a BuildingImage model with a OneToOne relation to BuildingType:
BuildImage Model:
/**
* Get the source type of the Building Image.
*/
public function type()
{
return $this->hasOne('AppBuildingType');
}
BuildingType Model:
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage');
}
My tables:
building_images table -> source is the building type id

building_types table

When I try to do this in my controller just to test:
(an ImageRequest has one or more Builings and a Building has one BuildingType)
$imageRequest = ImageRequest::findOrFail($id);
$buildings = $imageRequest->buildingImages;
foreach ($buildings as $building) {
dd($building->type);
}
I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'building_types.building_image_id' in 'where clause' (SQL: select *
frombuilding_typeswherebuilding_types.building_image_id= 45
andbuilding_types.building_image_idis not null limit 1)
What am I doing wrong here?
php laravel eloquent relationship
add a comment |
I have a BuildingImage model with a OneToOne relation to BuildingType:
BuildImage Model:
/**
* Get the source type of the Building Image.
*/
public function type()
{
return $this->hasOne('AppBuildingType');
}
BuildingType Model:
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage');
}
My tables:
building_images table -> source is the building type id

building_types table

When I try to do this in my controller just to test:
(an ImageRequest has one or more Builings and a Building has one BuildingType)
$imageRequest = ImageRequest::findOrFail($id);
$buildings = $imageRequest->buildingImages;
foreach ($buildings as $building) {
dd($building->type);
}
I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'building_types.building_image_id' in 'where clause' (SQL: select *
frombuilding_typeswherebuilding_types.building_image_id= 45
andbuilding_types.building_image_idis not null limit 1)
What am I doing wrong here?
php laravel eloquent relationship
add a comment |
I have a BuildingImage model with a OneToOne relation to BuildingType:
BuildImage Model:
/**
* Get the source type of the Building Image.
*/
public function type()
{
return $this->hasOne('AppBuildingType');
}
BuildingType Model:
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage');
}
My tables:
building_images table -> source is the building type id

building_types table

When I try to do this in my controller just to test:
(an ImageRequest has one or more Builings and a Building has one BuildingType)
$imageRequest = ImageRequest::findOrFail($id);
$buildings = $imageRequest->buildingImages;
foreach ($buildings as $building) {
dd($building->type);
}
I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'building_types.building_image_id' in 'where clause' (SQL: select *
frombuilding_typeswherebuilding_types.building_image_id= 45
andbuilding_types.building_image_idis not null limit 1)
What am I doing wrong here?
php laravel eloquent relationship
I have a BuildingImage model with a OneToOne relation to BuildingType:
BuildImage Model:
/**
* Get the source type of the Building Image.
*/
public function type()
{
return $this->hasOne('AppBuildingType');
}
BuildingType Model:
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage');
}
My tables:
building_images table -> source is the building type id

building_types table

When I try to do this in my controller just to test:
(an ImageRequest has one or more Builings and a Building has one BuildingType)
$imageRequest = ImageRequest::findOrFail($id);
$buildings = $imageRequest->buildingImages;
foreach ($buildings as $building) {
dd($building->type);
}
I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'building_types.building_image_id' in 'where clause' (SQL: select *
frombuilding_typeswherebuilding_types.building_image_id= 45
andbuilding_types.building_image_idis not null limit 1)
What am I doing wrong here?
php laravel eloquent relationship
php laravel eloquent relationship
edited Nov 15 '18 at 17:01
Ram Bhandari
361216
361216
asked Nov 15 '18 at 15:15
MichaelMichael
164110
164110
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Your BuildImage model should be
/**
* Get the source type of the Building Image.
*/
public function type() {
return $this->hasOne('AppBuildingType',"id","source");
}
And BuildingType Model should be
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage',"source","id");
}
This should work.
For more info have a look
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
add a comment |
That's because by default laravel will look for a primary key named {model}_id, and given that you are using a different column name (source), you need to specify when defining the relationship:
As the documentation states:
Eloquent determines the foreign key of the relationship based on the model name. In this case, the
Phonemodel is automatically assumed to have auser_idforeign key. If you wish to override this convention, you may pass a second argument to thehasOnemethod:
return $this->hasOne('AppPhone', 'foreign_key');
Additionally, Eloquent assumes that the foreign key should have a value matching the
id(or the custom$primaryKey) column of the parent. In other words, Eloquent will look for the value of the user's id column in theuser_idcolumn of thePhonerecord. If you would like the relationship to use a value other than id, you may pass a third argument to thehasOnemethod specifying your custom key:
return $this->hasOne('AppPhone', 'foreign_key', 'local_key');
Now that that is clear. Let's talk about the relationship itself.
You are defining that a BuildImage has one BuildingType. But with that logic, the foreign key should be stored in the building_types table, and not the other way around (source column appears in the building_images table). And -I'm just assuming that- many BuildImage can belongs to an specific BuildingType. So, if this assumption is correct:
- a
BuildImagebelongs to a specificBuildingType. - a
BuildinTypecan be specify in manyBuildImages
So, you should define your relationship like this:
BuildImage.php
public function type()
{
return $this->belongsTo('AppBuildingType', 'source');
}
BuildingType.php
public function images()
{
return $this->hasMany(BuildingImage::class, 'source');
}
So I have to fill in "source" in the "foreign_key" in your code example?
– Michael
Nov 15 '18 at 15:20
@Michael I'll update. But I thing that you should define your relationship a little bit different.
– HCK
Nov 15 '18 at 15:23
You can also use the class instead of a string, which some (myself included) tend to prefer.return $this->hasOne(AppPhone::class, 'foreign_key', 'local_key');
– John Halsey
Nov 15 '18 at 15:28
@JohnHalsey yes, indeed. Nice observation, I'll include it.
– HCK
Nov 15 '18 at 15:39
add a comment |
Have you tried to indicate the index ID like this?
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage', 'image_request_id');
}
Eloquent determines the foreign key of the relationship based on the
model name. In this case, the Phone model is automatically assumed to
have a user_id foreign key. If you wish to override this convention,
you may pass the second argument to the hasOne method:
return $this->hasOne('AppPhone', 'foreign_key');
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
Yes, same error
– Michael
Nov 15 '18 at 15:22
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%2f53322506%2flaravel-5-6-onetoone-relation-not-working%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
Your BuildImage model should be
/**
* Get the source type of the Building Image.
*/
public function type() {
return $this->hasOne('AppBuildingType',"id","source");
}
And BuildingType Model should be
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage',"source","id");
}
This should work.
For more info have a look
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
add a comment |
Your BuildImage model should be
/**
* Get the source type of the Building Image.
*/
public function type() {
return $this->hasOne('AppBuildingType',"id","source");
}
And BuildingType Model should be
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage',"source","id");
}
This should work.
For more info have a look
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
add a comment |
Your BuildImage model should be
/**
* Get the source type of the Building Image.
*/
public function type() {
return $this->hasOne('AppBuildingType',"id","source");
}
And BuildingType Model should be
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage',"source","id");
}
This should work.
For more info have a look
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
Your BuildImage model should be
/**
* Get the source type of the Building Image.
*/
public function type() {
return $this->hasOne('AppBuildingType',"id","source");
}
And BuildingType Model should be
/**
* Get the Building Image that owns the building type.
*/
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage',"source","id");
}
This should work.
For more info have a look
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
answered Nov 15 '18 at 15:26
Web ArtisanWeb Artisan
1,30531224
1,30531224
add a comment |
add a comment |
That's because by default laravel will look for a primary key named {model}_id, and given that you are using a different column name (source), you need to specify when defining the relationship:
As the documentation states:
Eloquent determines the foreign key of the relationship based on the model name. In this case, the
Phonemodel is automatically assumed to have auser_idforeign key. If you wish to override this convention, you may pass a second argument to thehasOnemethod:
return $this->hasOne('AppPhone', 'foreign_key');
Additionally, Eloquent assumes that the foreign key should have a value matching the
id(or the custom$primaryKey) column of the parent. In other words, Eloquent will look for the value of the user's id column in theuser_idcolumn of thePhonerecord. If you would like the relationship to use a value other than id, you may pass a third argument to thehasOnemethod specifying your custom key:
return $this->hasOne('AppPhone', 'foreign_key', 'local_key');
Now that that is clear. Let's talk about the relationship itself.
You are defining that a BuildImage has one BuildingType. But with that logic, the foreign key should be stored in the building_types table, and not the other way around (source column appears in the building_images table). And -I'm just assuming that- many BuildImage can belongs to an specific BuildingType. So, if this assumption is correct:
- a
BuildImagebelongs to a specificBuildingType. - a
BuildinTypecan be specify in manyBuildImages
So, you should define your relationship like this:
BuildImage.php
public function type()
{
return $this->belongsTo('AppBuildingType', 'source');
}
BuildingType.php
public function images()
{
return $this->hasMany(BuildingImage::class, 'source');
}
So I have to fill in "source" in the "foreign_key" in your code example?
– Michael
Nov 15 '18 at 15:20
@Michael I'll update. But I thing that you should define your relationship a little bit different.
– HCK
Nov 15 '18 at 15:23
You can also use the class instead of a string, which some (myself included) tend to prefer.return $this->hasOne(AppPhone::class, 'foreign_key', 'local_key');
– John Halsey
Nov 15 '18 at 15:28
@JohnHalsey yes, indeed. Nice observation, I'll include it.
– HCK
Nov 15 '18 at 15:39
add a comment |
That's because by default laravel will look for a primary key named {model}_id, and given that you are using a different column name (source), you need to specify when defining the relationship:
As the documentation states:
Eloquent determines the foreign key of the relationship based on the model name. In this case, the
Phonemodel is automatically assumed to have auser_idforeign key. If you wish to override this convention, you may pass a second argument to thehasOnemethod:
return $this->hasOne('AppPhone', 'foreign_key');
Additionally, Eloquent assumes that the foreign key should have a value matching the
id(or the custom$primaryKey) column of the parent. In other words, Eloquent will look for the value of the user's id column in theuser_idcolumn of thePhonerecord. If you would like the relationship to use a value other than id, you may pass a third argument to thehasOnemethod specifying your custom key:
return $this->hasOne('AppPhone', 'foreign_key', 'local_key');
Now that that is clear. Let's talk about the relationship itself.
You are defining that a BuildImage has one BuildingType. But with that logic, the foreign key should be stored in the building_types table, and not the other way around (source column appears in the building_images table). And -I'm just assuming that- many BuildImage can belongs to an specific BuildingType. So, if this assumption is correct:
- a
BuildImagebelongs to a specificBuildingType. - a
BuildinTypecan be specify in manyBuildImages
So, you should define your relationship like this:
BuildImage.php
public function type()
{
return $this->belongsTo('AppBuildingType', 'source');
}
BuildingType.php
public function images()
{
return $this->hasMany(BuildingImage::class, 'source');
}
So I have to fill in "source" in the "foreign_key" in your code example?
– Michael
Nov 15 '18 at 15:20
@Michael I'll update. But I thing that you should define your relationship a little bit different.
– HCK
Nov 15 '18 at 15:23
You can also use the class instead of a string, which some (myself included) tend to prefer.return $this->hasOne(AppPhone::class, 'foreign_key', 'local_key');
– John Halsey
Nov 15 '18 at 15:28
@JohnHalsey yes, indeed. Nice observation, I'll include it.
– HCK
Nov 15 '18 at 15:39
add a comment |
That's because by default laravel will look for a primary key named {model}_id, and given that you are using a different column name (source), you need to specify when defining the relationship:
As the documentation states:
Eloquent determines the foreign key of the relationship based on the model name. In this case, the
Phonemodel is automatically assumed to have auser_idforeign key. If you wish to override this convention, you may pass a second argument to thehasOnemethod:
return $this->hasOne('AppPhone', 'foreign_key');
Additionally, Eloquent assumes that the foreign key should have a value matching the
id(or the custom$primaryKey) column of the parent. In other words, Eloquent will look for the value of the user's id column in theuser_idcolumn of thePhonerecord. If you would like the relationship to use a value other than id, you may pass a third argument to thehasOnemethod specifying your custom key:
return $this->hasOne('AppPhone', 'foreign_key', 'local_key');
Now that that is clear. Let's talk about the relationship itself.
You are defining that a BuildImage has one BuildingType. But with that logic, the foreign key should be stored in the building_types table, and not the other way around (source column appears in the building_images table). And -I'm just assuming that- many BuildImage can belongs to an specific BuildingType. So, if this assumption is correct:
- a
BuildImagebelongs to a specificBuildingType. - a
BuildinTypecan be specify in manyBuildImages
So, you should define your relationship like this:
BuildImage.php
public function type()
{
return $this->belongsTo('AppBuildingType', 'source');
}
BuildingType.php
public function images()
{
return $this->hasMany(BuildingImage::class, 'source');
}
That's because by default laravel will look for a primary key named {model}_id, and given that you are using a different column name (source), you need to specify when defining the relationship:
As the documentation states:
Eloquent determines the foreign key of the relationship based on the model name. In this case, the
Phonemodel is automatically assumed to have auser_idforeign key. If you wish to override this convention, you may pass a second argument to thehasOnemethod:
return $this->hasOne('AppPhone', 'foreign_key');
Additionally, Eloquent assumes that the foreign key should have a value matching the
id(or the custom$primaryKey) column of the parent. In other words, Eloquent will look for the value of the user's id column in theuser_idcolumn of thePhonerecord. If you would like the relationship to use a value other than id, you may pass a third argument to thehasOnemethod specifying your custom key:
return $this->hasOne('AppPhone', 'foreign_key', 'local_key');
Now that that is clear. Let's talk about the relationship itself.
You are defining that a BuildImage has one BuildingType. But with that logic, the foreign key should be stored in the building_types table, and not the other way around (source column appears in the building_images table). And -I'm just assuming that- many BuildImage can belongs to an specific BuildingType. So, if this assumption is correct:
- a
BuildImagebelongs to a specificBuildingType. - a
BuildinTypecan be specify in manyBuildImages
So, you should define your relationship like this:
BuildImage.php
public function type()
{
return $this->belongsTo('AppBuildingType', 'source');
}
BuildingType.php
public function images()
{
return $this->hasMany(BuildingImage::class, 'source');
}
edited Nov 15 '18 at 15:45
answered Nov 15 '18 at 15:19
HCKHCK
3,57211138
3,57211138
So I have to fill in "source" in the "foreign_key" in your code example?
– Michael
Nov 15 '18 at 15:20
@Michael I'll update. But I thing that you should define your relationship a little bit different.
– HCK
Nov 15 '18 at 15:23
You can also use the class instead of a string, which some (myself included) tend to prefer.return $this->hasOne(AppPhone::class, 'foreign_key', 'local_key');
– John Halsey
Nov 15 '18 at 15:28
@JohnHalsey yes, indeed. Nice observation, I'll include it.
– HCK
Nov 15 '18 at 15:39
add a comment |
So I have to fill in "source" in the "foreign_key" in your code example?
– Michael
Nov 15 '18 at 15:20
@Michael I'll update. But I thing that you should define your relationship a little bit different.
– HCK
Nov 15 '18 at 15:23
You can also use the class instead of a string, which some (myself included) tend to prefer.return $this->hasOne(AppPhone::class, 'foreign_key', 'local_key');
– John Halsey
Nov 15 '18 at 15:28
@JohnHalsey yes, indeed. Nice observation, I'll include it.
– HCK
Nov 15 '18 at 15:39
So I have to fill in "source" in the "foreign_key" in your code example?
– Michael
Nov 15 '18 at 15:20
So I have to fill in "source" in the "foreign_key" in your code example?
– Michael
Nov 15 '18 at 15:20
@Michael I'll update. But I thing that you should define your relationship a little bit different.
– HCK
Nov 15 '18 at 15:23
@Michael I'll update. But I thing that you should define your relationship a little bit different.
– HCK
Nov 15 '18 at 15:23
You can also use the class instead of a string, which some (myself included) tend to prefer.
return $this->hasOne(AppPhone::class, 'foreign_key', 'local_key');– John Halsey
Nov 15 '18 at 15:28
You can also use the class instead of a string, which some (myself included) tend to prefer.
return $this->hasOne(AppPhone::class, 'foreign_key', 'local_key');– John Halsey
Nov 15 '18 at 15:28
@JohnHalsey yes, indeed. Nice observation, I'll include it.
– HCK
Nov 15 '18 at 15:39
@JohnHalsey yes, indeed. Nice observation, I'll include it.
– HCK
Nov 15 '18 at 15:39
add a comment |
Have you tried to indicate the index ID like this?
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage', 'image_request_id');
}
Eloquent determines the foreign key of the relationship based on the
model name. In this case, the Phone model is automatically assumed to
have a user_id foreign key. If you wish to override this convention,
you may pass the second argument to the hasOne method:
return $this->hasOne('AppPhone', 'foreign_key');
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
Yes, same error
– Michael
Nov 15 '18 at 15:22
add a comment |
Have you tried to indicate the index ID like this?
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage', 'image_request_id');
}
Eloquent determines the foreign key of the relationship based on the
model name. In this case, the Phone model is automatically assumed to
have a user_id foreign key. If you wish to override this convention,
you may pass the second argument to the hasOne method:
return $this->hasOne('AppPhone', 'foreign_key');
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
Yes, same error
– Michael
Nov 15 '18 at 15:22
add a comment |
Have you tried to indicate the index ID like this?
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage', 'image_request_id');
}
Eloquent determines the foreign key of the relationship based on the
model name. In this case, the Phone model is automatically assumed to
have a user_id foreign key. If you wish to override this convention,
you may pass the second argument to the hasOne method:
return $this->hasOne('AppPhone', 'foreign_key');
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
Have you tried to indicate the index ID like this?
public function buildingImage()
{
return $this->belongsTo('AppBuildingImage', 'image_request_id');
}
Eloquent determines the foreign key of the relationship based on the
model name. In this case, the Phone model is automatically assumed to
have a user_id foreign key. If you wish to override this convention,
you may pass the second argument to the hasOne method:
return $this->hasOne('AppPhone', 'foreign_key');
https://laravel.com/docs/5.7/eloquent-relationships#one-to-one
edited Nov 15 '18 at 15:22
answered Nov 15 '18 at 15:21
DeimosDeimos
134415
134415
Yes, same error
– Michael
Nov 15 '18 at 15:22
add a comment |
Yes, same error
– Michael
Nov 15 '18 at 15:22
Yes, same error
– Michael
Nov 15 '18 at 15:22
Yes, same error
– Michael
Nov 15 '18 at 15:22
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%2f53322506%2flaravel-5-6-onetoone-relation-not-working%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