Laravel get models two descendant levels via pivot tables
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am using Laravel v4.1... yes I know it is old. My question is how to set up my models & go about getting related models that are "two" levels away & associated by two pivot tables.
For example...
My database tables:
firsts:
id name
== =============
1 lorem
2 ipsum
seconds:
id name
== =============
3 dolor
4 sit
thirds:
id name
== =============
5 amet
6 consectetur
7 adipiscing
8 elit
first_second:
first_id second_id
======== =========
1 3
2 4
second_third:
second_id third_id
========= ========
3 5
3 6
4 7
4 8
If FirstModel is 1, then I would like to get the following from ThirdModel:
id name
== =============
5 amet
6 consectetur
I can do it for one level away (ie, SecondModel). Any ideas as to how to do it for ThirdModel?
laravel laravel-4
add a comment |
I am using Laravel v4.1... yes I know it is old. My question is how to set up my models & go about getting related models that are "two" levels away & associated by two pivot tables.
For example...
My database tables:
firsts:
id name
== =============
1 lorem
2 ipsum
seconds:
id name
== =============
3 dolor
4 sit
thirds:
id name
== =============
5 amet
6 consectetur
7 adipiscing
8 elit
first_second:
first_id second_id
======== =========
1 3
2 4
second_third:
second_id third_id
========= ========
3 5
3 6
4 7
4 8
If FirstModel is 1, then I would like to get the following from ThirdModel:
id name
== =============
5 amet
6 consectetur
I can do it for one level away (ie, SecondModel). Any ideas as to how to do it for ThirdModel?
laravel laravel-4
add a comment |
I am using Laravel v4.1... yes I know it is old. My question is how to set up my models & go about getting related models that are "two" levels away & associated by two pivot tables.
For example...
My database tables:
firsts:
id name
== =============
1 lorem
2 ipsum
seconds:
id name
== =============
3 dolor
4 sit
thirds:
id name
== =============
5 amet
6 consectetur
7 adipiscing
8 elit
first_second:
first_id second_id
======== =========
1 3
2 4
second_third:
second_id third_id
========= ========
3 5
3 6
4 7
4 8
If FirstModel is 1, then I would like to get the following from ThirdModel:
id name
== =============
5 amet
6 consectetur
I can do it for one level away (ie, SecondModel). Any ideas as to how to do it for ThirdModel?
laravel laravel-4
I am using Laravel v4.1... yes I know it is old. My question is how to set up my models & go about getting related models that are "two" levels away & associated by two pivot tables.
For example...
My database tables:
firsts:
id name
== =============
1 lorem
2 ipsum
seconds:
id name
== =============
3 dolor
4 sit
thirds:
id name
== =============
5 amet
6 consectetur
7 adipiscing
8 elit
first_second:
first_id second_id
======== =========
1 3
2 4
second_third:
second_id third_id
========= ========
3 5
3 6
4 7
4 8
If FirstModel is 1, then I would like to get the following from ThirdModel:
id name
== =============
5 amet
6 consectetur
I can do it for one level away (ie, SecondModel). Any ideas as to how to do it for ThirdModel?
laravel laravel-4
laravel laravel-4
asked Jan 3 at 10:27
user1822391user1822391
13029
13029
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
According to the doc in 4.1 there was has-many-through, so you might try something like this:
class FirstModel extends ModelAbstract
{
public function seconds()
{
return $this->belongsToMany(SecondModel::class);
}
public function thirds()
{
return $this->hasManyThrough(ThirdModel::class, SecondModel::class);
}
}
class SecondModel extends ModelAbstract
{
public function thirds()
{
return $this->belongsToMany(ThirdModel::class);
}
}
class ThirdModel extends ModelAbstract {}
And then get the data for third relationship from the first like this:
$first = FirstModel::find(1);
$thirds = $first->thirds;
If that doesn't work, you could do it the old fashion way where you collect all third item ids from the relationships and query the manually:
$first = FirstModel::with(['seconds.thirds'])->find(1);
$thirdIds = $first->seconds->map(function ($item) {
return $item->thirds->pluck('id')->toArray();
})->toArray();
$thirds = ThirdModel::whereIn('id', array_flatten($thirdIds));
add a comment |
@thefallen,
Not sure why neither your first method nor your second method did not worked for me. I ended up going with a hybrid of the two with some extra loops. Not the cleanest / prettiest but it gets the job done.
$results = ;
$id = 1 ;
$first = FirstModel :: with( [ 'seconds.thirds' ] ) -> find( $id ) ;
$seconds = $first -> seconds -> map( function ( $item ) {
return $item -> thirds ;
} ) -> toArray() ;
foreach ( $seconds as $item1 ) {
foreach ( $item1 as $item2 ) {
$results[ $item2[ 'id' ] ] = $item2[ 'name' ] ;
}
}
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%2f54020439%2flaravel-get-models-two-descendant-levels-via-pivot-tables%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
According to the doc in 4.1 there was has-many-through, so you might try something like this:
class FirstModel extends ModelAbstract
{
public function seconds()
{
return $this->belongsToMany(SecondModel::class);
}
public function thirds()
{
return $this->hasManyThrough(ThirdModel::class, SecondModel::class);
}
}
class SecondModel extends ModelAbstract
{
public function thirds()
{
return $this->belongsToMany(ThirdModel::class);
}
}
class ThirdModel extends ModelAbstract {}
And then get the data for third relationship from the first like this:
$first = FirstModel::find(1);
$thirds = $first->thirds;
If that doesn't work, you could do it the old fashion way where you collect all third item ids from the relationships and query the manually:
$first = FirstModel::with(['seconds.thirds'])->find(1);
$thirdIds = $first->seconds->map(function ($item) {
return $item->thirds->pluck('id')->toArray();
})->toArray();
$thirds = ThirdModel::whereIn('id', array_flatten($thirdIds));
add a comment |
According to the doc in 4.1 there was has-many-through, so you might try something like this:
class FirstModel extends ModelAbstract
{
public function seconds()
{
return $this->belongsToMany(SecondModel::class);
}
public function thirds()
{
return $this->hasManyThrough(ThirdModel::class, SecondModel::class);
}
}
class SecondModel extends ModelAbstract
{
public function thirds()
{
return $this->belongsToMany(ThirdModel::class);
}
}
class ThirdModel extends ModelAbstract {}
And then get the data for third relationship from the first like this:
$first = FirstModel::find(1);
$thirds = $first->thirds;
If that doesn't work, you could do it the old fashion way where you collect all third item ids from the relationships and query the manually:
$first = FirstModel::with(['seconds.thirds'])->find(1);
$thirdIds = $first->seconds->map(function ($item) {
return $item->thirds->pluck('id')->toArray();
})->toArray();
$thirds = ThirdModel::whereIn('id', array_flatten($thirdIds));
add a comment |
According to the doc in 4.1 there was has-many-through, so you might try something like this:
class FirstModel extends ModelAbstract
{
public function seconds()
{
return $this->belongsToMany(SecondModel::class);
}
public function thirds()
{
return $this->hasManyThrough(ThirdModel::class, SecondModel::class);
}
}
class SecondModel extends ModelAbstract
{
public function thirds()
{
return $this->belongsToMany(ThirdModel::class);
}
}
class ThirdModel extends ModelAbstract {}
And then get the data for third relationship from the first like this:
$first = FirstModel::find(1);
$thirds = $first->thirds;
If that doesn't work, you could do it the old fashion way where you collect all third item ids from the relationships and query the manually:
$first = FirstModel::with(['seconds.thirds'])->find(1);
$thirdIds = $first->seconds->map(function ($item) {
return $item->thirds->pluck('id')->toArray();
})->toArray();
$thirds = ThirdModel::whereIn('id', array_flatten($thirdIds));
According to the doc in 4.1 there was has-many-through, so you might try something like this:
class FirstModel extends ModelAbstract
{
public function seconds()
{
return $this->belongsToMany(SecondModel::class);
}
public function thirds()
{
return $this->hasManyThrough(ThirdModel::class, SecondModel::class);
}
}
class SecondModel extends ModelAbstract
{
public function thirds()
{
return $this->belongsToMany(ThirdModel::class);
}
}
class ThirdModel extends ModelAbstract {}
And then get the data for third relationship from the first like this:
$first = FirstModel::find(1);
$thirds = $first->thirds;
If that doesn't work, you could do it the old fashion way where you collect all third item ids from the relationships and query the manually:
$first = FirstModel::with(['seconds.thirds'])->find(1);
$thirdIds = $first->seconds->map(function ($item) {
return $item->thirds->pluck('id')->toArray();
})->toArray();
$thirds = ThirdModel::whereIn('id', array_flatten($thirdIds));
answered Jan 3 at 14:13
thefallenthefallen
5,72511740
5,72511740
add a comment |
add a comment |
@thefallen,
Not sure why neither your first method nor your second method did not worked for me. I ended up going with a hybrid of the two with some extra loops. Not the cleanest / prettiest but it gets the job done.
$results = ;
$id = 1 ;
$first = FirstModel :: with( [ 'seconds.thirds' ] ) -> find( $id ) ;
$seconds = $first -> seconds -> map( function ( $item ) {
return $item -> thirds ;
} ) -> toArray() ;
foreach ( $seconds as $item1 ) {
foreach ( $item1 as $item2 ) {
$results[ $item2[ 'id' ] ] = $item2[ 'name' ] ;
}
}
add a comment |
@thefallen,
Not sure why neither your first method nor your second method did not worked for me. I ended up going with a hybrid of the two with some extra loops. Not the cleanest / prettiest but it gets the job done.
$results = ;
$id = 1 ;
$first = FirstModel :: with( [ 'seconds.thirds' ] ) -> find( $id ) ;
$seconds = $first -> seconds -> map( function ( $item ) {
return $item -> thirds ;
} ) -> toArray() ;
foreach ( $seconds as $item1 ) {
foreach ( $item1 as $item2 ) {
$results[ $item2[ 'id' ] ] = $item2[ 'name' ] ;
}
}
add a comment |
@thefallen,
Not sure why neither your first method nor your second method did not worked for me. I ended up going with a hybrid of the two with some extra loops. Not the cleanest / prettiest but it gets the job done.
$results = ;
$id = 1 ;
$first = FirstModel :: with( [ 'seconds.thirds' ] ) -> find( $id ) ;
$seconds = $first -> seconds -> map( function ( $item ) {
return $item -> thirds ;
} ) -> toArray() ;
foreach ( $seconds as $item1 ) {
foreach ( $item1 as $item2 ) {
$results[ $item2[ 'id' ] ] = $item2[ 'name' ] ;
}
}
@thefallen,
Not sure why neither your first method nor your second method did not worked for me. I ended up going with a hybrid of the two with some extra loops. Not the cleanest / prettiest but it gets the job done.
$results = ;
$id = 1 ;
$first = FirstModel :: with( [ 'seconds.thirds' ] ) -> find( $id ) ;
$seconds = $first -> seconds -> map( function ( $item ) {
return $item -> thirds ;
} ) -> toArray() ;
foreach ( $seconds as $item1 ) {
foreach ( $item1 as $item2 ) {
$results[ $item2[ 'id' ] ] = $item2[ 'name' ] ;
}
}
answered Jan 4 at 6:16
user1822391user1822391
13029
13029
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%2f54020439%2flaravel-get-models-two-descendant-levels-via-pivot-tables%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