0

in this below code i used array_push in the main function

$postsList = [];
foreach ($categories->posts as $post) {
    array_push($postsList, $post);
}

and after this operation i used this code:

$this->child($categories,$postsList);

child is a function which into that i used $postsList and my problem is i can't return latest value of that to main function

full code:

public function index()
{
    ...
    $postsList = [];
    foreach ($categories->posts as $post) {
        array_push($postsList, $post);
    }


    array_push($postsList, $this->child($categories, $postsList));
    dd($postsList);
}

private function child($categories,$postsList)
{
    if ($categories->childrenCategories->first()) {
        foreach ($categories->childrenCategories as $childCategory) {
            foreach ($childCategory->categories as $category) {
                foreach ($category->posts as $post) {
                    array_push($postsList,$post);
                }
            }

            foreach ($childCategory->posts as $post) {
                array_push($postsList,$post);
            }
            $this->child($childCategory,$postsList);
        }
    }
}

2 Answers 2

2

You can manage them by sending the same copy of postsList using reference & like below in the function parameter.

private function child($categories,&$postsList)
{
    if ($categories->childrenCategories->first()) {
        foreach ($categories->childrenCategories as $childCategory) {
            foreach ($childCategory->categories as $category) {
                foreach ($category->posts as $post) {
                    array_push($postsList,$post);
                }
            }

            foreach ($childCategory->posts as $post) {
                array_push($postsList,$post);
            }
            $this->child($childCategory,$postsList);
        }
    }
}

In index() method, you don't need to array_push() again from the child() call, but rather just call the child() method.

public function index()
{
    ...
    $postsList = [];
    foreach ($categories->posts as $post) {
        array_push($postsList, $post);
    }


    $this->child($categories, $postsList);
    dd($postsList);
}
Sign up to request clarification or add additional context in comments.

Comments

1

From what I understand is that you are trying to pluck out the posts from categories nested under root categories.

Try the below


//Eager load all relations on the Category records

$categories = Category::with(['posts', 'childrenCategories.posts', 'childrenCategories.categories.posts'])->get();

public function getPosts($categories)
{
      //Get all Posts from the root level categories
      $postList = collect($categories->pluck('posts')->all());

      $categories->each(function($category) use(&$postList){

        if ($category->childrenCategories->count()) {

            $category->childrenCategories->each(function ($childCategroy) use (&$postList){
                //Get all Posts from child categories of root level categories
                $postList->push($childCategroy->posts);

                //Get all Posts from the categories of child category
                $postList->push($childCategroy->categories->pluck('posts')->all());
            });
        }

      });

      return $postList->unique('id')->all();
}

Then in the main function you can just call getPosts($categories)

public function main()
{
    $postList = $this->getPosts($categories);
}

Another way this could be accomplished it to make a query to get ids of all categories with nested categories. Then make a simple query to the posts table to get all posts where category_id is within the array.

9 Comments

posts and categories are ManyToMany and passed categories has posts in relations and $categories->pluck('posts') return Column not found: 1054 Unknown column 'posts' in 'field list' (SQL: select posts` from categories where categories.deleted_at is null)` error
If you have eager loaded posts on categories then pluck should work. $categories = Category::with('posts')->get() then $categories->pluck('posts') should not give any errors like column not found, because then we are using pluck on a collection of categories with posts reation
@Donkarnashmy Query is : $categories = Category::with(['childrenCategories' => function ($query) {$query->with('posts');}, 'posts'])
@DolDurma Have updated answer to show eager load all relations (sample code). Let me know if it works
i checked, this code which i used $categories = Category::with(['posts', 'childrenCategories.posts', 'childrenCategories.categories.posts'])->get(); return 233 row and when i try to dump it as dd($categories->childrenCategories); i get Property [childrenCategories] does not exist on this collection instance. error. in my model i have childrenCategories not childCategories
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.