Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 2 years ago.
0

Try this

$cat = Category::where('parent_id', '=', null)->get()
$cat_kids = $cat->subCategories();
$cat_parent = $cat->parent();
Last updated 2 years ago.
0

Fetching subCategories and the parent category from already built model objects works fine, the problem is querying as I've described above.

Last updated 2 years ago.
0

has and whereHas are used to check if there exist at least one related model looking from the One-side to the Many-side.
Check out the generated query. You will understand the error.
At the moment I can not think of an easy solution

Last updated 2 years ago.
0
Category::whereNotNull('parent_id')->get();

That should do it. As for the other part of the question...writing this off the top of my head so it might need adjusting:

Category::select(DB::raw('pr_category.*, (select c2.slug from pr_category AS c2 where c2.id =  pr_category.parent_id) as parentSlug'))->whereNotNull('parent_id')->where('parentSlug', $parentSlug)->get();

'pr_' being whatever your prefix is. Disclaimer: this might not work xD

Last updated 2 years ago.
0

Hi,

Was this ever solved. Attempting now and just want to be sure.

Thanks

0

Sign in to participate in this thread!

Eventy

Your banner here too?

mvrkljan mvrkljan Joined 30 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.