<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NEONOS .NET &#187; Laravel</title>
	<atom:link href="https://neonos.net/category/programming/php/laravel/feed/" rel="self" type="application/rss+xml" />
	<link>https://neonos.net</link>
	<description>&#38; ONE DAY TECHNOLOGY WILL SET US ALL FREE</description>
	<lastBuildDate>Tue, 13 Jan 2015 20:05:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Laravel Eloquent Model Parent/Child Relationship with itself</title>
		<link>https://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/</link>
		<comments>https://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/#comments</comments>
		<pubDate>Sun, 04 Jan 2015 03:15:01 +0000</pubDate>
		<dc:creator><![CDATA[neo]]></dc:creator>
				<category><![CDATA[Eager Loading]]></category>
		<category><![CDATA[Eloquent]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Self Relationship]]></category>

		<guid isPermaLink="false">http://neonos.net/?p=42</guid>
		<description><![CDATA[Unconditional love has a lot to do with a parents upbringing, or else you gotta tell them who their Daddy is! This relationship model helped me bring my SQL queries in my web page from over a 100 down to 6, it might help some people out: note: the dependency_id column will contain the ID [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Unconditional love has a lot to do with a parents upbringing, or else you gotta tell them who their Daddy is! This relationship model helped me bring my SQL queries in my web page from over a 100 down to 6, it might help some people out:</p>
<blockquote><p>note: the dependency_id column will contain the ID of the parent, you can modify this to have many to many relations using pivot tables!</p></blockquote>
<pre class="prettyprint" style="padding-left: 30px;">
&lt;?php

class Person extends \Eloquent {
 protected $fillable = &#91;&#93;;
 var $mom, $kids;
 
 function __construct() { 
   if($this-&gt;dependency_id&lt;&gt;0) {
     $this-&gt;mother-&gt;with(&#39;mother&#39;); 
   }
 }
 
public function children() {
   $children = $this-&gt;hasMany(&#39;Person&#39;,&#39;dependency_id&#39;);
   foreach($children as $child) {
     $child-&gt;mom = $this;
   }
   return $children;
}
public function mother() {
 $mother = $this-&gt;belongsTo(&#39;Person&#39;,&#39;dependency_id&#39;);
  if(isset($mother-&gt;kids)) {
    $mother-&gt;kids-&gt;merge($mother);
  }
  return $mother;
}
 
public function canonical_url($type=&#39;soft&#39;) {
 if($type==&#39;hard&#39;) {
  return
     Config::get(&#39;app.url&#39;).
     (isset($this-&gt;mother-&gt;mother-&gt;slug)?$this-&gt;mother-&gt;mother-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     (isset($this-&gt;mother-&gt;slug)?$this-&gt;mother-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     $this-&gt;slug;
 }else {
   return
     Config::get(&#39;app.url&#39;).
     (isset($this-&gt;mom-&gt;mom-&gt;slug)?$this-&gt;mom-&gt;mom-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     (isset($this-&gt;mom-&gt;slug)?$this-&gt;mom-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     $this-&gt;slug;
   }
 }
}

</pre>
<p>Now you can do things like this:</p>
<pre class="prettyprint">&lt;!--?php    $page = Page::with(&#39;children&#39;,&#39;children.children&#39;)---&gt;where(&#39;slug&#39;,&#39;=&#39;, $page_slug)-&gt;first();
?&gt;
Then in your view file:

&lt;code&gt;&lt;ul class=&#34;nav navbar-nav&#34;&gt;
@foreach($navigation as $parent_item)
&lt;li class=&#34;@if(count($parent_item-&gt;children)) dropdown @endif @if($parent_item-&gt;canonical_url()==Request::path()) active @endif&#34; &gt;
&lt;a href=&#34;&#47;{{$parent_item-&gt;slug}}&#34;&gt;
{{$parent_item-&gt;name}}
&lt;&#47;a&gt;
@if(count($parent_item-&gt;children))
&lt;ul class=&#34;dropdown-menu&#34; role=&#34;menu&#34; aria-labelledby=&#34;dLabel&#34;&gt;

@foreach($parent_item-&gt;children as $child_item)
&lt;li &gt;
&lt;a href=&#34;{{$child_item-&gt;canonical_url()}}&#34;&gt;
{{$child_item-&gt;name}}
&lt;br&gt;
&lt;span&gt;{{$child_item-&gt;description}}&lt;&#47;span&gt;
&lt;&#47;a&gt;

@if(count($child_item-&gt;children))
&lt;ul class=&#34;dropdown-menu&#34;&gt;
&lt;li data-bird=&#39;{{$child_item-&gt;summary}}&#39;&gt;
&lt;a href=&#34;{{$child_item-&gt;canonical_url()}}&#34;&gt;
{{$child_item-&gt;title}} Overview
&lt;&#47;a&gt;
&lt;&#47;li&gt;
@foreach($child_item-&gt;children as $grandchild_item)
&lt;li &gt;
&lt;a href=&#34;{{$grandchild_item-&gt;canonical_url()}}&#34;&gt;
{{$grandchild_item-&gt;name}}
&lt;br&gt;
&lt;span&gt;{{$grandchild_item-&gt;description}}&lt;&#47;span&gt;
&lt;&#47;a&gt;
&lt;&#47;li&gt;
@endforeach
&lt;&#47;ul&gt;
@endif
&lt;&#47;li&gt;
@endforeach
&lt;&#47;ul&gt;
@endif
&lt;&#47;li&gt;
@endforeach
&lt;&#47;ul&gt;&lt;&#47;code&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>https://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
