HasMany Deep RelationshipGet local href value from anchor (a) tagClass design for relationshipsHow to fetch specific columns from multiple tables in Laravel 5?Conversion from query builder to eloquent model with 3 tablesLaravel 5 Hasmanythrough inverse queryingLaravel eloquent relationship fetch records upto third level of relationshipFetch data from Laravel query builder with with elquent relationshipNesting hasMany() relationships in LaravelLaravel Eloquent ORM hasMany where other pivoted relationship existsLaravel & relationshipshasMany, belongsTo, belongsToMany parameters with custom names?

Get contents before a colon

How do I portray irrational anger in first person?

Cauterizing a wound with metal?

Fixing a blind bolt hole when the first 2-3 threads are ruined?

Where should I draw the line on follow up questions from previous employer

Why is there no Disney logo in MCU movies?

Why does the weaker C–H bond have a higher wavenumber than the C=O bond?

What is the purpose of Strength, Intelligence and Dexterity in Path of Exile?

Board Chinese train at a different station (on-route)

Is "survival" paracord with fire starter strand dangerous

Can a network vulnerability be exploited locally?

How do you say "half the time …, the other half …" in German?

Historical Daf Yomi calendar

Under GDPR, can I give permission once to allow everyone to store and process my data?

What does "-1" represent in the value range for unsigned int and signed int?

How can I fix cracks between the bathtub and the wall surround?

How can I reply to coworkers who accuse me of automating people out of work?

is "prohibition against," a double negative?

What is this "opened" cube called?

Coupling two 15 Amp circuit breaker for 20 Amp

Is there a word or phrase that means "use other people's wifi or Internet service without consent"?

How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?

Did the Apollo Guidance Computer really use 60% of the world's ICs in 1963?

Can a Sorcerer use the Careful Spell Metamagic option on spells with optional saving throws?



HasMany Deep Relationship


Get local href value from anchor (a) tagClass design for relationshipsHow to fetch specific columns from multiple tables in Laravel 5?Conversion from query builder to eloquent model with 3 tablesLaravel 5 Hasmanythrough inverse queryingLaravel eloquent relationship fetch records upto third level of relationshipFetch data from Laravel query builder with with elquent relationshipNesting hasMany() relationships in LaravelLaravel Eloquent ORM hasMany where other pivoted relationship existsLaravel & relationshipshasMany, belongsTo, belongsToMany parameters with custom names?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have 5 models with one pivot table Country Province City Area Tour tour_location. How to achieve below functionality?



$country->tours



$province->tours



$city->tours



$area->tours



Country.php HasMany Provinces



public function provinces()

return $this->hasMany('AppProvince', 'country_id', 'id');



Province.php HasMany Cities



public function cities()

return $this->hasMany('AppCity', 'province_id', 'id');



City.php HasMany Areas



public function areas()

return $this->hasMany('AppArea', 'city_id', 'id');



Area.php BelongsToMany Tours



public function tours()

return $this->belongsToMany('AppTour', 'tour_locations');










share|improve this question


























  • You can ckeck this link stackoverflow.com/questions/15439853/…

    – Manisha
    Mar 28 at 12:51

















0















I have 5 models with one pivot table Country Province City Area Tour tour_location. How to achieve below functionality?



$country->tours



$province->tours



$city->tours



$area->tours



Country.php HasMany Provinces



public function provinces()

return $this->hasMany('AppProvince', 'country_id', 'id');



Province.php HasMany Cities



public function cities()

return $this->hasMany('AppCity', 'province_id', 'id');



City.php HasMany Areas



public function areas()

return $this->hasMany('AppArea', 'city_id', 'id');



Area.php BelongsToMany Tours



public function tours()

return $this->belongsToMany('AppTour', 'tour_locations');










share|improve this question


























  • You can ckeck this link stackoverflow.com/questions/15439853/…

    – Manisha
    Mar 28 at 12:51













0












0








0








I have 5 models with one pivot table Country Province City Area Tour tour_location. How to achieve below functionality?



$country->tours



$province->tours



$city->tours



$area->tours



Country.php HasMany Provinces



public function provinces()

return $this->hasMany('AppProvince', 'country_id', 'id');



Province.php HasMany Cities



public function cities()

return $this->hasMany('AppCity', 'province_id', 'id');



City.php HasMany Areas



public function areas()

return $this->hasMany('AppArea', 'city_id', 'id');



Area.php BelongsToMany Tours



public function tours()

return $this->belongsToMany('AppTour', 'tour_locations');










share|improve this question
















I have 5 models with one pivot table Country Province City Area Tour tour_location. How to achieve below functionality?



$country->tours



$province->tours



$city->tours



$area->tours



Country.php HasMany Provinces



public function provinces()

return $this->hasMany('AppProvince', 'country_id', 'id');



Province.php HasMany Cities



public function cities()

return $this->hasMany('AppCity', 'province_id', 'id');



City.php HasMany Areas



public function areas()

return $this->hasMany('AppArea', 'city_id', 'id');



Area.php BelongsToMany Tours



public function tours()

return $this->belongsToMany('AppTour', 'tour_locations');







php laravel relationship






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 22:25









Kenny Horna

5,9221 gold badge14 silver badges45 bronze badges




5,9221 gold badge14 silver badges45 bronze badges










asked Mar 27 at 22:18









FayFay

411 silver badge14 bronze badges




411 silver badge14 bronze badges















  • You can ckeck this link stackoverflow.com/questions/15439853/…

    – Manisha
    Mar 28 at 12:51

















  • You can ckeck this link stackoverflow.com/questions/15439853/…

    – Manisha
    Mar 28 at 12:51
















You can ckeck this link stackoverflow.com/questions/15439853/…

– Manisha
Mar 28 at 12:51





You can ckeck this link stackoverflow.com/questions/15439853/…

– Manisha
Mar 28 at 12:51












1 Answer
1






active

oldest

votes


















1















The direct way is do it with joins, another way is to make a custom relationship extending the hasManyThrough(). The third option -imo- is to use the Eloquent-has-many-deep package.



Using this package, you could do this:



class Country extends Model

use StaudenmeirEloquentHasManyDeepHasRelationships;

public function tours()

return $this
->hasManyDeep('AppTour', ['AppProvince', 'AppCity', 'AppArea', 'area_tour']);




Then in your controller:



// ...
$country = Country::find(1);
$tours = $country->tours;


Disclaimer: I'm not involved in this package in any way. I'm just suggesting it because is the simplest way to achieve your desired behavior.






share|improve this answer



























  • Well this make sense, thanks @HCK

    – Fay
    Mar 27 at 22:30











  • yes Area has pivot table, but anyhow i got the idea.

    – Fay
    Mar 27 at 22:34











  • @Fay I've updated the method with -I think- is the right way to set up your relationships. Give it a try and tell me if this worked.

    – Kenny Horna
    Mar 27 at 22:35












  • its working like a charm.

    – Fay
    Mar 27 at 22:59










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387333%2fhasmany-deep-relationship%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















The direct way is do it with joins, another way is to make a custom relationship extending the hasManyThrough(). The third option -imo- is to use the Eloquent-has-many-deep package.



Using this package, you could do this:



class Country extends Model

use StaudenmeirEloquentHasManyDeepHasRelationships;

public function tours()

return $this
->hasManyDeep('AppTour', ['AppProvince', 'AppCity', 'AppArea', 'area_tour']);




Then in your controller:



// ...
$country = Country::find(1);
$tours = $country->tours;


Disclaimer: I'm not involved in this package in any way. I'm just suggesting it because is the simplest way to achieve your desired behavior.






share|improve this answer



























  • Well this make sense, thanks @HCK

    – Fay
    Mar 27 at 22:30











  • yes Area has pivot table, but anyhow i got the idea.

    – Fay
    Mar 27 at 22:34











  • @Fay I've updated the method with -I think- is the right way to set up your relationships. Give it a try and tell me if this worked.

    – Kenny Horna
    Mar 27 at 22:35












  • its working like a charm.

    – Fay
    Mar 27 at 22:59















1















The direct way is do it with joins, another way is to make a custom relationship extending the hasManyThrough(). The third option -imo- is to use the Eloquent-has-many-deep package.



Using this package, you could do this:



class Country extends Model

use StaudenmeirEloquentHasManyDeepHasRelationships;

public function tours()

return $this
->hasManyDeep('AppTour', ['AppProvince', 'AppCity', 'AppArea', 'area_tour']);




Then in your controller:



// ...
$country = Country::find(1);
$tours = $country->tours;


Disclaimer: I'm not involved in this package in any way. I'm just suggesting it because is the simplest way to achieve your desired behavior.






share|improve this answer



























  • Well this make sense, thanks @HCK

    – Fay
    Mar 27 at 22:30











  • yes Area has pivot table, but anyhow i got the idea.

    – Fay
    Mar 27 at 22:34











  • @Fay I've updated the method with -I think- is the right way to set up your relationships. Give it a try and tell me if this worked.

    – Kenny Horna
    Mar 27 at 22:35












  • its working like a charm.

    – Fay
    Mar 27 at 22:59













1














1










1









The direct way is do it with joins, another way is to make a custom relationship extending the hasManyThrough(). The third option -imo- is to use the Eloquent-has-many-deep package.



Using this package, you could do this:



class Country extends Model

use StaudenmeirEloquentHasManyDeepHasRelationships;

public function tours()

return $this
->hasManyDeep('AppTour', ['AppProvince', 'AppCity', 'AppArea', 'area_tour']);




Then in your controller:



// ...
$country = Country::find(1);
$tours = $country->tours;


Disclaimer: I'm not involved in this package in any way. I'm just suggesting it because is the simplest way to achieve your desired behavior.






share|improve this answer















The direct way is do it with joins, another way is to make a custom relationship extending the hasManyThrough(). The third option -imo- is to use the Eloquent-has-many-deep package.



Using this package, you could do this:



class Country extends Model

use StaudenmeirEloquentHasManyDeepHasRelationships;

public function tours()

return $this
->hasManyDeep('AppTour', ['AppProvince', 'AppCity', 'AppArea', 'area_tour']);




Then in your controller:



// ...
$country = Country::find(1);
$tours = $country->tours;


Disclaimer: I'm not involved in this package in any way. I'm just suggesting it because is the simplest way to achieve your desired behavior.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 22:34

























answered Mar 27 at 22:25









Kenny HornaKenny Horna

5,9221 gold badge14 silver badges45 bronze badges




5,9221 gold badge14 silver badges45 bronze badges















  • Well this make sense, thanks @HCK

    – Fay
    Mar 27 at 22:30











  • yes Area has pivot table, but anyhow i got the idea.

    – Fay
    Mar 27 at 22:34











  • @Fay I've updated the method with -I think- is the right way to set up your relationships. Give it a try and tell me if this worked.

    – Kenny Horna
    Mar 27 at 22:35












  • its working like a charm.

    – Fay
    Mar 27 at 22:59

















  • Well this make sense, thanks @HCK

    – Fay
    Mar 27 at 22:30











  • yes Area has pivot table, but anyhow i got the idea.

    – Fay
    Mar 27 at 22:34











  • @Fay I've updated the method with -I think- is the right way to set up your relationships. Give it a try and tell me if this worked.

    – Kenny Horna
    Mar 27 at 22:35












  • its working like a charm.

    – Fay
    Mar 27 at 22:59
















Well this make sense, thanks @HCK

– Fay
Mar 27 at 22:30





Well this make sense, thanks @HCK

– Fay
Mar 27 at 22:30













yes Area has pivot table, but anyhow i got the idea.

– Fay
Mar 27 at 22:34





yes Area has pivot table, but anyhow i got the idea.

– Fay
Mar 27 at 22:34













@Fay I've updated the method with -I think- is the right way to set up your relationships. Give it a try and tell me if this worked.

– Kenny Horna
Mar 27 at 22:35






@Fay I've updated the method with -I think- is the right way to set up your relationships. Give it a try and tell me if this worked.

– Kenny Horna
Mar 27 at 22:35














its working like a charm.

– Fay
Mar 27 at 22:59





its working like a charm.

– Fay
Mar 27 at 22:59








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387333%2fhasmany-deep-relationship%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현