How to get the count and values of item in a subcategory with parent_id in Laravel

display category and subcategory in laravel
laravel eloquent parent child same table
category subcategory product laravel
laravel recursive relationship
laravel 5 category treeview hierarchical structure example with demo
laravel category subcategory tree
laravel parent child category
laravel recursive query

How do I get the count and values of all items within a subcategory that has a parent_id. I have a category model which is related to the product model. And in the category table, there are parent_ids and their subcategories. How do I return all the products belonging to a parent category?

The database is shown below Database

The Relationships are shown here

public function products()
{
    return $this->hasMany('App\Product');
}

public function category()
{
    return $this->belongsTo('App\Category');
}

Then in my controller, this returns 0 as count

public function niche($url)
{
    $niche = Category::where('url',$url)->first();

    $categories = Category::where('parent_id',$niche->id)->get();
    $products = [];

    foreach($categories as $category) {
        $products = Product::where('category_id',$category->id)->get();
    }

    dd($products->count());
}

Output

Please how do I get the number of products belonging to this niche? Thanks.


I think products() is a relationship method of products and categories

Can you try like this:

$products = [];

foreach($categories as $category){
    array_push($products, $category->products());
}

dd(count($products, true));

How to get the count and values of item in a subcategory , How to get the count and values of item in a subcategory with parent_id in Laravel. 226. December 07, 2019, at 02:00 AM. How do I get the count and values of� This is my code of inserting data into database


I think this is happening because every time you go through a loop you create a new product variable, thus it will always dump out the count of the last category in the loop.

You should try something like this:

public function niche($url){
$niche = Category::where('url',$url)->first();


$categories = Category::where('parent_id',$niche->id)->get();
$products = [];


foreach($categories as $category){
    array_push($products,$category->products());    
}

dd($products->count());
}

Eloquent: Recursive hasMany Relationship with , We just have a name field, and then relationship to the table itself. NULL, and every other sub-category will have its own parent_id. We have unlimited level of subcategories – in database, in Eloquent relationships, and in Views. value as a validation I covered everything in the parent item value. And I can add a new category which belongs to an already created category. Something like “Test -> subcategory of test” and then let’s say I will create “sub-subcategory of test” which belongs to subcategory of test which will look like this: “Test -> subcategory of test -> sub-subcategory of test”


I think the result is 0 maybe because the last sub category doesn't have any product.

Btw, I suggest a bit cleaner approach in this base. Lets define a sub categories relationship for a category.

class Category extends Model
{
    public function products()
    {
        return $this->hasMany('App\Product');
    }

    public function subCategories()
    {
        return $this->hasMany(static::class, 'parent_id');
    }
}

So you are able to access the products and the number of products in a number of ways:

// You can eager load for performance.
$category = Category::with('subCategories.products')->where('url', $url)->first();

$products = $category->subCategories->flatMap->products;
$count = $products->count();

// Or you can eager load JUST the number of products.
$category = Category::withCount('subCategories.products')->where('url', $url)->first();
$numberOfProducts = $category->products_count;

get category list (including multiple subcategories) of an article, get category list (including multiple subcategories) of an article I have 2 tables: one categories table with an id, name and a parent_id, and one So in Laravel I want to use something like $article->categorySlug which gives me this string. quite work yet, I get the error: Trying to get property of non-object. How to get the count and values of item in a subcategory with parent_id in Laravel. READ ALSO. Java. Getting registry keys with JMI.


Get all products from category(and sub-categories), trying to get all the products of a category and from its subcategories. categories id | parent_id | name 1 | NULL | Computer 2 | 1 | Hardware I can get the product of the next and previous category level doing this: Another good one https://laracasts.com/discuss/channels/laravel/complex-query-for-a-� Laravel query builder doesn't return all prices ba Laravel 5.1 custom model does not generate a table; i need to store the id in a selected row in laravel; Cannot insert values into database in laravel 5.1; how to integrate laravel 5.1 with elasticsearch 2.1.1; lavacharts' doesn't display chart size properly us Three-way Eloquent


COUNT of rows with parent id equal to the row we have with a , This is easily achievable with an in-line subquery : select m.id, (select count(*) from messages where parent_id= m.id ) as ChildCount from messages m where � I have item which has following fields id, description, value, parent_id, calculated value what would be your approach to have automatic categorisation and sums of items so say if item is parent to other item it will summarise the values of all children and than go up to the grand total. ex Category A 500 item 1-CA 200 item 2-CA 300 item 1-1-CA 170


Laravel get all parents of category, I have categories and subcategories in laravel namespace App; use public function parent() { return $this->belongsTo('App\Category', 'parent_id') can fetch all the parents of a category #if(count($category->parents)) <td>{{ Try checking if checking if $i->item->item->item_images is empty before trying to use the value� I am having an issue with a Laravel project. I have a few checkbox of computer specification which I want to check against the database field that I am having called tech_specs. This tech_specs field contains the serialize array of all the tech_specs that are available for that specific product. I just want to list out the products having that