title: SQL to JSON, ORM for PHP 7 and Laravel
body: <p>I am working on a REST API where I have to query information and return a proper JSON object. I am looking for PHP/Laravel libraries which can do the job easily.</p>
  
  <p>A normal SQL query with table JOINs gives me rows of information</p>
  
  <p><code>SQL</code>:</p>
  
  <pre><code>    SELECT H.fname AS `fname`,
      H.sname AS `sname`,
      S.id AS `students.id`,
      A.topic AS `students.tasks.topic`,
      A.wordcount AS `students.tasks.wordcount`
      FROM `humans` AS `H`
      LEFT JOIN `students` AS `S` ON H.id = S.human
      LEFT JOIN `courses` AS `C` ON S.course = C.id
      LEFT JOIN `tasks` AS `T` ON T.student = S.id
      LEFT JOIN `assignments` AS `A` ON T.assignment = A.id
      WHERE H.email = "user@email.com"
  </code></pre>
  
  <p>The response rows encoded in JSON:</p>
  
  <pre><code>[
      {
          "fname": "Donnie",
          "sname": "Ashok",
          "students.id": "987152e7b3056f1b96c964614b0b6328",
          "students.tasks.topic": "How to dismantle an Anti-matter injector manifold? ",
          "students.tasks.wordcount": 1500
      },
      {
          "fname": "Donnie",
          "sname": "Ashok",
          "students.id": "987152e7b3056f1b96c964614b0b6328",
          "students.tasks.topic": "How to intercept sub-space transmission using Dilithium Matrices? ",
          "students.tasks.wordcount": 1500
      },
      {
          "fname": "Donnie",
          "sname": "Ashok",
          "students.id": "3ea4f7b2e1e4bcc26ae544dd14d107fe",
          "students.tasks.topic": null,
          "students.tasks.wordcount": null
      }
  ]
  </code></pre>
  
  <p>However, I need something like a proper JSON object like this:</p>
  
  <pre><code>[
      {
          "fname": "Donnie",
          "sname": "Ashok",
          "students": [
              {
                  "id": "987152e7b3056f1b96c964614b0b6328",
                  "tasks": [
                      {
                          "topic": "How to intercept sub-space transmission using Dilithium Matrices? ",
                          "wordcount": 1500
                      },
                      {
                          "topic": "How to dismantle an Anti-matter injector manifold?",
                          "wordcount": 1500
                      }
                  ]
              },
              {
                  "id": "3ea4f7b2e1e4bcc26ae544dd14d107fe",
                  "tasks": []
              }
          ]
      }
  ]
  </code></pre>
  
  <p>Is there any PHP library or ORM which I can use to organise the SQL rows?</p>