last day (14 days later) » 

8:00 AM
1
A: Laravel 5 - Elequent GROUP BY is failing

jedrzej.kuryloIf I understand correctly, you'd like to fetch a list of Content objects together with their children Content objects, correct? Easiest way to do that is to create a parent-child relation in your Eloquent Content model and then use that to load parents with children: <?php class Content extends...

 
Thank you for this. It is sadly returning nothing.. But I think that is because the foreign key on 'Content' has not been set up.. So parent_id will have to be a foreign key to the other records inside the table?
 
Yes. For children you have to set it to the id of the parent content id. For parent content objects it should be set to null.
 
Nope, it doesn't work.. The foreign keys are set up for: parent_id -> for other sections inside the Content table AND section_id foreign key for ids inside the section table?
 
Could you update the questions with how you define the foreign keys? I think the formatting gets broken when above comment is rendered, I'm not sure what you mean.
 
I have updated the question.. I think I'm doing it wrong since, parent_id is referenced to the id on the same table.. But I think it has to be parent_id as a foreign key to parent_id but when I try to add this relationship, I get an error: 150 - Foreign key constraint is incorrectly formed)
 
8:00 AM
"parent_id" needs to point to "id" on the same table. This is how you can implement tree structure in a relational database. Could you elaborate on what is not working when you do it like that?
 
Hi, I'm getting somewhere..
$section = Content::with('children')->whereNull('parent_id')->get();
foreach($section as $section) { var_dump($section->name); } gives me null
But I don't get, in the Content model, where is the section defined? (Since section is another table)
 
why do you call it a section? I think section is another model in your database?
 
Yes, there is a Content, and a Section
 
I'm lost :) Do you want a parent content with its children or content with their section??
 
these are linked by a foreign key in Content "section_id"
Me too.. 1 sec let me try and explain this :P
 
8:03 AM
::with(['children', 'section']) will give you both children and section
 
I have a database table called "Content" and a table called "Section" .. Content has 1 section.. BUT the table "Content" can have many sub contents which have a section.. So for example... Content: id => 1, section_id => 2, parent_id = NULL
but if I add another section: id => 2, section_id => 3, parent_id => 1
this is now linked to Content with ID 1.. and should display under it, with it as it relates to it.. Does that make sense?
 
so... you want a list of parent Contents and their Section together with Children and their Section?
 
yeahh exactly :)
 
ok, updating the answer :)
 
thank you
hopefully this will work as it's been hurting my brain ha
 
8:08 AM
try it now, I updated the calls to ::with()
 
Call to undefined method Illuminate\Database\Query\Builder::section() :P
 
where do you get it?
 
$contents = DigitalContent::with(['children', 'section', 'children.section'])->whereNull('parent_id')->get();
 
did you define "section" relation?
 
$contents = Content::with(['children', 'section', 'children.section'])->whereNull('parent_id')->get();
 
8:12 AM
but did you define section() method in your content model?
I updated the answer and added that
 
ok, i think i've got this :D
1 sec though
Okay, now printing it out.. The way on your answer does not produce anything other than bullet points.. If I do this though in blade:
<ul>
@foreach($contents as $content)
<?php var_dump($content->section->name); ?>
@endforeach
</ul>
produces the correct name
Found it :) works now, thank you so so much for your help man!
 
np
please just mark the answer as correct
 
ill mark it as accepted :)
have you been doing laravel for long btw?
 
since february
but those are Eloquent's basics
just o throught the docs and you'd know how to answer your question :)
 
aha yeahh like your answer, I would never have got it
thank you anyway :) have an amazing day/night
 

last day (14 days later) »