how to eager loading nested relationships in Laravel with custom query? - php

I have a comments table, with theses fields: id, body, and parent_id. comments can have subcomments, that's why it is using the parent_id field, I would like to know how I can achieve this nested result:
[
{
"id": 1,
"body": "test",
"parent_id": null,
"comments": [
{
"id": 2,
"body": "test",
"parent_id": 1,
"comments": [
{
"id": 3,
"body": "test",
"parent_id": 2
},
{
"id": 4,
"body": "test",
"parent_id": 2
}
]
},
{
"id": 5,
"body": "test",
"parent_id": 1,
"comments": []
}
]
},
{
"id": 6,
"body": "test",
"parent_id": null,
"comments": []
}
]
Without using eloquent eager loading(with()), just using the query builder, thank you.

Since the possible answer could be too long, I am posting it directly as an answer. You could achieve this by creating a recursive method in your controller like below:
public function commentsLoop($comments, $parent = null)
{
$result = [];
foreach ($comments as $comment) {
if ($comment['parent_id'] === $parent) {
$subComment = commentsLoop($comments, $comment['id']);
if ($subComment) {
$comment['comments'] = $subComment;
}
$result[] = $comment;
}
}
return $result;
}
And then, you can call it from your main method in your controller like below:
public function comments()
{
$comments = DB::table('comments')->all()->toArray();
return $this->commentsLoop($comments);
}
However, if you would use eloquent instead, creating a relationship to self in your model would be the answer.

Related

Generate Nested Tree Structure or Hierarchy based on parent child relationship using recursion - PHP

I've been stuck in this since yesterday any help will be much appreciated. The situation is this: I have a table for navigation links with the following structure,
id | parent_id | text
In my scenario the parent_id may be null(no sub-menu) or any id pointing back to the table which will make the record a sub-menu of the parent. I've tried many different ways to execute this but with no luck, the closest scenario I came up with is the following,
// All navigation link results are stored in $data
public static function transform(array &$data) {
$result = [];
$search = function(&$array = [], $parent = null) use (&$search, &$result){
foreach($array as $key => $value) {
/** If the element has an ancestor */
if($parent && $parent->id == $value->parent_id) {
$next = $parent->children[] = $value;
$search($array, $next);
unset($array[$key]);
}
}
return $parent;
};
foreach($data as $val) {
$result[] = $search($data, $val);
}
return $result;
}
The function works fine and tracks all the ancestors of a certain link but in the next iteration is tracking all the ancestors of the next element(even if it was a child of the previous one) so it comes back with a lot of duplicated data. I came up with the solution to unset the iterated element from the array but for some reason, the element is still being used in the next iteration.
Here is my current data
Here is my current result
A simple concept for faster search and to process one item only once.
Create a temp array with key as id and values as array of child ids. Will use this array to process next direct childs. It would be a depth first traversal.
Also set id as key in input array, so we can directly access whole node when required.
function generateTree($data){
$arrChild = []; // Store parent as key and its childs as array to quickly find out.
foreach($data as $obj){
$arrChild[$obj->parent_id][] = $obj->id;
$data[$obj->id] = $obj;
}
$final = [];
$setChild = function(&$array, $parents) use (&$setChild, $data, $arrChild){
foreach($parents as $parent){
$temp = $data[$parent];
// If key is set, that means given node has direct childs, so process them too.
if(isset($arrChild[$parent])){
$temp->children = [];
$setChild($temp->children, $arrChild[$parent]);
}
$array[] = $temp;
}
};
// Empty key would represent nodes with parent as `null`
$setChild($final, $arrChild['']);
return $final;
}
$final = generateTree($arr);
echo json_encode($final, JSON_PRETTY_PRINT);
Output:
[
{
"id": 1,
"navigation_id": 4,
"parent_id": null,
"text": "link 1",
"icon": "",
"url": null,
"page_id": 4,
"children": [
{
"id": 2,
"navigation_id": 4,
"parent_id": 1,
"text": "link 2",
"icon": "",
"url": null,
"page_id": 4,
"children": [
{
"id": 3,
"navigation_id": 4,
"parent_id": 2,
"text": "link 3",
"icon": "fas fa-ad",
"url": "https:\/\/google.com",
"page_id": null
},
{
"id": 4,
"navigation_id": 4,
"parent_id": 2,
"text": "link 4",
"icon": "fab fa-google",
"url": "https:\/\/google.com",
"page_id": null
}
]
}
]
},
{
"id": 5,
"navigation_id": 4,
"parent_id": null,
"text": "link 5",
"icon": "",
"url": null,
"page_id": 5,
"children": [
{
"id": 6,
"navigation_id": 4,
"parent_id": 5,
"text": "link 6",
"icon": "",
"url": null,
"page_id": 4
},
{
"id": 7,
"navigation_id": 4,
"parent_id": 5,
"text": "link 7",
"icon": "",
"url": null,
"page_id": 4
}
]
}
]

Modifiy response in laravel

Below my Laravel controller code
public function templateValueData($template_id)
{
$parent = TemplateMetadataValue::where('template_id', $template_id)->get();
return $parent;
}
Here, the output of the method above;
[
{
"id": 11,
"parent_id": 0,
"name": "Category 1"
},
{
"id": 63,
"parent_id": 11,
"name": "Category 2"
},
{
"id": 73,
"parent_id": 63,
"name": "Category 3"
},
{
"id": 77,
"parent_id": 11,
"name": "Category 5"
},
{
"id": 83,
"parent_id": 77,
"name": "Category 8"
}
]
Now I want to format this response base on the value of parent_id.
The condition is: when parent_id is equal to id, then the parent_id is considered as children of the id.
Please note that both parent_id and id are stored as incremental values.
I want to alter and return the response as below;
[
{
"id": 11,
"parent_id": 0,
"name": "Category 1",
"children": [
{
"id": 63,
"parent_id": 11,
"name": "Category 2",
"children": [
{
"id": 73,
"parent_id": 63,
"name": "Category 3"
}
]
},
{
"id": 77,
"parent_id": 11,
"name": "Category 5",
"children": [
{
"id": 83,
"parent_id": 77,
"name": "Category 8"
}
]
}
]
}
]
How can I achieve this?
Use below code for achieve your response.
public function templateValueData($template_id)
{
$parents = TemplateMetadataValue::where('template_id', $template_id)->get();
$arr = $this->xyz($parents);
return $arr;
}
Function for achieve response.
function xyz($parents,$parent_id = 0){
$res = [];
foreach ( $parents as $parent ) {
if ( $parent[ "parent_id"] == $parent_id ){
$children = $this->xyz($parents, $parent[ "id"]);
if ($children) {
$parent[ "children"] = $children;
}
$res[]=$parent;
}
}
return $res;
}
You should call your relation on your controller:
public function templateValueData($template_id)
{
$parent = TemplateMetadataValue::where('template_id',$template_id)->with('children')->get();
return $parent;
}
Also you can use method injection on your controller methods, so you can use this code:
public function templateValueData(TemplateMetadataValue $template)
{
return $template->load('children')->get();
}
If you slightly modify your TemplateMetadataValue model then you could achieve the desired result.
class TemplateMetadataValue extends Model {
public function children() {
return $this->hasMany(static::class, 'parent_id');
}
}
Call it with children method.
$parent = TemplateMetadataValue::with('children')->get();
Rest of the login you can apply here with your query builder.

Laravel model belongs to current user and global user

I have the following collections:
User
{
"id": 1,
"name": "Bob"
},
{
"id": 2,
"name": "Mary"
}
Stuff
{
"id": 1,
"user_id": 1,
"name": "Bob's stuff"
},
{
"id": 2,
"user_id": 2,
"name": "Mary's stuff"
},
{
"id": 3,
"user_id": 0,
"name": "Everyone's stuff"
},
{
"id": 4,
"user_id": null,
"name": "Global stuff"
}
... and the following models:
class User extends Eloquent {
public function stuff() {
return $this -> hasMany( 'Stuff' ) ;
}
}
class Stuff extends Eloquent {
public function user() {
return $this -> belongsTo( 'User' ) ;
}
}
How to I automatically add Everyone's stuff ( User.id == 0 ) and Global stuff ( User.id == null ) to everybody's model? In other words, if I was querying for Bob's stuff I wish to get the following:
{
"id": 1,
"user_id": 1,
"name": "Bob's stuff"
},
{
"id": 3,
"user_id": 0,
"name": "Everyone's stuff"
},
{
"id": 4,
"user_id": null,
"name": "Global stuff"
}
You can use this:
public function stuff() {
return $this->hasMany(Stuff::class)->orWhere('user_id', 0)->orWhereNull('user_id');
}
A disadvantage is that you can't just add other WHERE clauses:
$user->stuff()->where('foo', 'bar')->get(); // Doesn't work as expected.
As a consequence, eager loading doesn't work.

How to select specific fields from nested relations in Eloquent

I have the following query in Eloquent:
public function firstSubsectionIdsOnly()
{
return $this->model->with(['sections' => function ($q) {
$q->with(['subsections' => function ($q2) {
$q2->first();
}
])->first();
}])->whereHas('sections.subsections')->first();
}
This returns something like this:
{
"id": 1,
"name": "Training exercise",
"entry": "<p>sss</p>",
"created_at": "2018-04-20 09:38:36",
"updated_at": "2018-04-20 10:08:27",
"sections": [
{
"id": 1,
"name": "Section 1 Training",
"parent": null,
"position": 1,
"created_at": "2018-05-04 09:37:23",
"updated_at": "2018-05-04 09:37:23",
"pivot": {
"training_exercise_id": 1,
"section_id": 1
},
"subsections": [
{
"id": 2,
"name": "Subsection 1 training",
"parent": 1,
"created_at": "2018-05-04 09:54:09",
"updated_at": "2018-05-04 09:54:09"
}
]
}
]
}
I would like for it to only select the id fields for each relation so it should return something like this:
{
"id": 1,
"name": "Training exercise",
"entry": "<p>sss</p>",
"created_at": "2018-04-20 09:38:36",
"updated_at": "2018-04-20 10:08:27",
"sections": [
{
"id": 1,
"subsections": [
{
"id": 2,
}
]
}
]
}
I have tried adding $q->select('id'); to the subsections nested closure but that returns an empty subsections array.
Any idea on how I can achieve this? I am using Laravel 5.6
As others have stated before, you need to include the foreign key in the values you select. Let's say we have two models. Parent and Child. This is the bare minimum you have to select.
App\Parent::select('id')->with(['children' => function ($query) {
$query->select('id', 'parent_id');
}])->get();
It nets you something like
Illuminate\Database\Eloquent\Collection {
all: [
App\Parent {
id: 1,
children: Illuminate\Database\Eloquent\Collection {
all: [
App\Child {
id: 109,
parent_id: 1,
},
App\Child {
id: 153,
parent_id: 1,
},
],
},
},
],
}
If you're trying to go for the inverse, you still need the foreign key, like so
App\Child::select('id', 'parent_id')->with('parent' => function ($query) {
$query->select('id');
}])->get();
This nets you
Illuminate\Database\Eloquent\Collection {
all: [
App\Child {
id: 1,
parent_id: 58,
parent: App\Parent {
id: 58,
},
},
],
}
I'm using select() instead of get() for legibility on the queries but you must use select() for the subqueries.
If you use with('relation') => function ($query) { $query->get(['id']); } you'll get the whole object.
You have to include the foreign key columns. For the subsections relationship:
$q2->select('id', 'parent');

Laravel - pluck fields from relations

i'm new in laravel and php and now i wanna create a rest api
I have this query:
$questions = Question::with(
array('picture' => function($query){
$query>select('question_id','picture_name')>pluck('picture_name')>all();
},
'user'=>function($query){
$query->select('id','image','name');
}))->get()->toArray();
my query return this json:
{
"questions:": [
{
"id": 1,
"title": "23",
"caption": "last",
"address": "lsdbabfd",
"picture": [
{
"question_id": 1,
"picture_name": "1527484678.jpg"
},
{
"question_id": 1,
"picture_name": "1527485120.jpg"
}
],
"user": {
"id": 1,
"image": "defaultPicture",
"name": "alex"
}
}
]
}
but i want this json:
{
"questions:": [
{
"id": 1,
"title": "23",
"caption": "last",
"address": "lsdbabfd",
"picture": [
"1527484678.jpg",
"1527485120.jpg"
],
"user": {
"id": 1,
"image": "defaultPicture",
"name": "alex"
}
}
]
}
how can i do that?
i already tried to do that with Laravel pluck method and Eloquent Mutators
you can add attribute method
class Question extends Model
{
...
protected $appends = ['pictures'];
public function getPicturesAttribute()
{
return $this->pictures->pluck('picture_id');
}
}

Categories