Eloquent eager load constrain by parent column values not working
I have this following tables;
shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
and below is the Models;
class Shipment extends Model {
protected $table = 'shipment';
public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}
class Item extends Model {
protected $table = 'item';
}
I tried to get the data for API;
return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();
but I am getting empty items
. But weirdly I am getting the items
data when I am using this; Shipment::find($id)->items
. What did I miss?
laravel laravel-5 eloquent
add a comment |
I have this following tables;
shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
and below is the Models;
class Shipment extends Model {
protected $table = 'shipment';
public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}
class Item extends Model {
protected $table = 'item';
}
I tried to get the data for API;
return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();
but I am getting empty items
. But weirdly I am getting the items
data when I am using this; Shipment::find($id)->items
. What did I miss?
laravel laravel-5 eloquent
It's not possible to use$this
in a relationship in combination with eager loading.
– Jonas Staudenmeir
Nov 22 '18 at 14:43
@JonasStaudenmeir Any workaround? I am trying to display theitems
, but I don't want to update the table structure because it's already correct to me.
– sulaiman sudirman
Nov 22 '18 at 15:41
Take a look at this package: github.com/topclaudy/compoships
– Jonas Staudenmeir
Nov 22 '18 at 15:43
@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.
– sulaiman sudirman
Nov 22 '18 at 16:56
add a comment |
I have this following tables;
shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
and below is the Models;
class Shipment extends Model {
protected $table = 'shipment';
public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}
class Item extends Model {
protected $table = 'item';
}
I tried to get the data for API;
return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();
but I am getting empty items
. But weirdly I am getting the items
data when I am using this; Shipment::find($id)->items
. What did I miss?
laravel laravel-5 eloquent
I have this following tables;
shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0
and below is the Models;
class Shipment extends Model {
protected $table = 'shipment';
public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}
class Item extends Model {
protected $table = 'item';
}
I tried to get the data for API;
return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();
but I am getting empty items
. But weirdly I am getting the items
data when I am using this; Shipment::find($id)->items
. What did I miss?
laravel laravel-5 eloquent
laravel laravel-5 eloquent
asked Nov 22 '18 at 13:15


sulaiman sudirmansulaiman sudirman
1,2111422
1,2111422
It's not possible to use$this
in a relationship in combination with eager loading.
– Jonas Staudenmeir
Nov 22 '18 at 14:43
@JonasStaudenmeir Any workaround? I am trying to display theitems
, but I don't want to update the table structure because it's already correct to me.
– sulaiman sudirman
Nov 22 '18 at 15:41
Take a look at this package: github.com/topclaudy/compoships
– Jonas Staudenmeir
Nov 22 '18 at 15:43
@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.
– sulaiman sudirman
Nov 22 '18 at 16:56
add a comment |
It's not possible to use$this
in a relationship in combination with eager loading.
– Jonas Staudenmeir
Nov 22 '18 at 14:43
@JonasStaudenmeir Any workaround? I am trying to display theitems
, but I don't want to update the table structure because it's already correct to me.
– sulaiman sudirman
Nov 22 '18 at 15:41
Take a look at this package: github.com/topclaudy/compoships
– Jonas Staudenmeir
Nov 22 '18 at 15:43
@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.
– sulaiman sudirman
Nov 22 '18 at 16:56
It's not possible to use
$this
in a relationship in combination with eager loading.– Jonas Staudenmeir
Nov 22 '18 at 14:43
It's not possible to use
$this
in a relationship in combination with eager loading.– Jonas Staudenmeir
Nov 22 '18 at 14:43
@JonasStaudenmeir Any workaround? I am trying to display the
items
, but I don't want to update the table structure because it's already correct to me.– sulaiman sudirman
Nov 22 '18 at 15:41
@JonasStaudenmeir Any workaround? I am trying to display the
items
, but I don't want to update the table structure because it's already correct to me.– sulaiman sudirman
Nov 22 '18 at 15:41
Take a look at this package: github.com/topclaudy/compoships
– Jonas Staudenmeir
Nov 22 '18 at 15:43
Take a look at this package: github.com/topclaudy/compoships
– Jonas Staudenmeir
Nov 22 '18 at 15:43
@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.
– sulaiman sudirman
Nov 22 '18 at 16:56
@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.
– sulaiman sudirman
Nov 22 '18 at 16:56
add a comment |
2 Answers
2
active
oldest
votes
It's not possible to use $this
in a relationship in combination with eager loading.
Take a look at this package: https://github.com/topclaudy/compoships
add a comment |
that relation wont work correctly cause the structure is incorrect. try it like this
shipments
==========
id date document_no address_id
----------------------------
1 2018-11-20 SO-18-11971 0
2 2018-11-20 SO-18-11971 1
3 2018-11-21 SO-18-11972 0
item
=======
shipment_id
-------------------------------------
1
2
3
Less redundancy and more efficient. the relation will be
class Shipment extends Model {
public function items()
{
return $this->hasMany(Item::class);
}
}
take in count in my example i changed the table name from shipment
to shipments
so you dont need to declare the table or the foreign key.
that said, Eloquent (and Laravel) doesn't support multi key index in relations.
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%2f53431848%2feloquent-eager-load-constrain-by-parent-column-values-not-working%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
It's not possible to use $this
in a relationship in combination with eager loading.
Take a look at this package: https://github.com/topclaudy/compoships
add a comment |
It's not possible to use $this
in a relationship in combination with eager loading.
Take a look at this package: https://github.com/topclaudy/compoships
add a comment |
It's not possible to use $this
in a relationship in combination with eager loading.
Take a look at this package: https://github.com/topclaudy/compoships
It's not possible to use $this
in a relationship in combination with eager loading.
Take a look at this package: https://github.com/topclaudy/compoships
answered Nov 22 '18 at 17:22


Jonas StaudenmeirJonas Staudenmeir
12.9k2934
12.9k2934
add a comment |
add a comment |
that relation wont work correctly cause the structure is incorrect. try it like this
shipments
==========
id date document_no address_id
----------------------------
1 2018-11-20 SO-18-11971 0
2 2018-11-20 SO-18-11971 1
3 2018-11-21 SO-18-11972 0
item
=======
shipment_id
-------------------------------------
1
2
3
Less redundancy and more efficient. the relation will be
class Shipment extends Model {
public function items()
{
return $this->hasMany(Item::class);
}
}
take in count in my example i changed the table name from shipment
to shipments
so you dont need to declare the table or the foreign key.
that said, Eloquent (and Laravel) doesn't support multi key index in relations.
add a comment |
that relation wont work correctly cause the structure is incorrect. try it like this
shipments
==========
id date document_no address_id
----------------------------
1 2018-11-20 SO-18-11971 0
2 2018-11-20 SO-18-11971 1
3 2018-11-21 SO-18-11972 0
item
=======
shipment_id
-------------------------------------
1
2
3
Less redundancy and more efficient. the relation will be
class Shipment extends Model {
public function items()
{
return $this->hasMany(Item::class);
}
}
take in count in my example i changed the table name from shipment
to shipments
so you dont need to declare the table or the foreign key.
that said, Eloquent (and Laravel) doesn't support multi key index in relations.
add a comment |
that relation wont work correctly cause the structure is incorrect. try it like this
shipments
==========
id date document_no address_id
----------------------------
1 2018-11-20 SO-18-11971 0
2 2018-11-20 SO-18-11971 1
3 2018-11-21 SO-18-11972 0
item
=======
shipment_id
-------------------------------------
1
2
3
Less redundancy and more efficient. the relation will be
class Shipment extends Model {
public function items()
{
return $this->hasMany(Item::class);
}
}
take in count in my example i changed the table name from shipment
to shipments
so you dont need to declare the table or the foreign key.
that said, Eloquent (and Laravel) doesn't support multi key index in relations.
that relation wont work correctly cause the structure is incorrect. try it like this
shipments
==========
id date document_no address_id
----------------------------
1 2018-11-20 SO-18-11971 0
2 2018-11-20 SO-18-11971 1
3 2018-11-21 SO-18-11972 0
item
=======
shipment_id
-------------------------------------
1
2
3
Less redundancy and more efficient. the relation will be
class Shipment extends Model {
public function items()
{
return $this->hasMany(Item::class);
}
}
take in count in my example i changed the table name from shipment
to shipments
so you dont need to declare the table or the foreign key.
that said, Eloquent (and Laravel) doesn't support multi key index in relations.
answered Nov 22 '18 at 13:24


N69SN69S
1,125514
1,125514
add a comment |
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%2f53431848%2feloquent-eager-load-constrain-by-parent-column-values-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
It's not possible to use
$this
in a relationship in combination with eager loading.– Jonas Staudenmeir
Nov 22 '18 at 14:43
@JonasStaudenmeir Any workaround? I am trying to display the
items
, but I don't want to update the table structure because it's already correct to me.– sulaiman sudirman
Nov 22 '18 at 15:41
Take a look at this package: github.com/topclaudy/compoships
– Jonas Staudenmeir
Nov 22 '18 at 15:43
@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.
– sulaiman sudirman
Nov 22 '18 at 16:56