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

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript