title: Laravel ORM from self referencing table get N level hierarchy JSON
body: <p>I am using <code>LARAVEL 4</code> with <code>MySQL</code> back-end. </p>
  
  <p>I have a <code>self-referencing</code> table with columns <code>id, name, type</code> and <code>parent</code>.
  Here, <code>parent</code> is <code>foreign-key</code> of the column <code>Id</code>. The data in table is as below : </p>
  
  <pre><code>id  name          type         parent 
  1   General       group        NULL
  2   What is..?    question     1
  3   aa            answer       2
  4   bb            answer       2
  5   cc            answer       2
  6   How is..?     question     1
  7   ba            answer       6
  8   bb            answer       6
  9   Where is..?   question     4
  10  ca            answer       9
  11  cb            answer       9
  12  Who is..?     question     6
  13  da            answer       12
  14  db            answer       12
  15  Specific      group        NULL
  16  When is..?    question     15
  17  ea            answer       16
  18  eb            answer       16
  19  Whome is..?   question     2
  20  fa            answer       19
  21  fb            answer       19
  22  fc            answer       19
  </code></pre>
  
  <p>I want a function that return <code>nested JSON</code> using this relational data. For example : </p>
  
  <pre><code>[{
    "id" : 1, 
    "name" : "Geneal", 
    "type" : "group", 
    "children" : [{
        "id" : 2, 
        "name" : "What is..?", 
        "type" : "question", 
        "children" : [{
           "id" : 3, 
           "name" : "aa", 
           "type" : "answer"
        },
        {
           "id" : 4, 
           "name" : "bb", 
           "type" : "answer"
        },
        {
           "id" : 5, 
           "name" : "cc", 
           "type" : "answer"
        }]},
        {
        "id" : 6, 
        "name" : "How is..?", 
        "type" : "question", 
        "children" : [{
           "id" : 7, 
           "name" : "ba", 
           "type" : "answer"
        },
        {
           "id" : 8, 
           "name" : "bb", 
           "type" : "answer"
        }]
     }]
  ... and so on
  }]
  </code></pre>
  
  <p>I have created a <code>model</code> named <code>Survey</code> as below : </p>
  
  <pre><code>class Survey extends BaseModel{
  
      protected $table = 'questions';
      protected $softDelete = false;
  
      public function parent()
      {
          return $this-&gt;belongsTo('Survey', 'parent');
      }
  
      public function children()
      {
          return $this-&gt;hasMany('Survey', 'parent');
      }   
  }
  </code></pre>
  
  <p>and called it in <code>controller</code> with : </p>
  
  <pre><code>$user = Survey::all();
  $parent = $user-&gt;parent()-&gt;first();
  $children = $user-&gt;children()-&gt;get();
  </code></pre>
  
  <p>But I am not getting the proper result as I have mentioned in <code>JSON</code> above. </p>
  
  <pre><code>print_r($parent-&gt;toJson()); 
  </code></pre>
  
  <p>gives records with one level hierarchy only (i.e group and questions, not answers). </p>
  
  <p>while </p>
  
  <pre><code>print_r($children -&gt;toJson());
  </code></pre>
  
  <p>Gives only questions (Not groups and answers). </p>
  
  <p>I want the whole <strong>self-referencing data in nested JSON format with N level of hierarchy</strong>. </p>
  
  <p>I also have tried </p>
  
  <pre><code>$user = Survey::with('parent', 'children')-&gt;get();
  </code></pre>
  
  <p>But found same as $parent above. </p>
  
  <p>Is there anyway I can get the desired result?</p>
  
  <p>Thanks in advance..</p>