staudenmeir
Repos
27
Followers
1052
Following
3

Laravel Eloquent HasManyThrough relationships with unlimited levels

2004
135

Recursive Laravel Eloquent relationships with CTEs

769
66

Laravel Eloquent BelongsToThrough relationships

747
69

Laravel Eloquent relationships with JSON keys

671
47

Laravel Eloquent eager loading with limit

614
46

Laravel queries with common table expressions

301
21

Events

Category has many manufacturers from product brands

There must be multiple paths from the category through the category_product table to this manufacturer (possibly with different products and/or brands). The query result is correct, but they are also ways to only get unique manufacturers.

What does your query look like in this case?

Created at 2 days ago
issue comment
[9.x] Possible fix for unexpected column in SQL Server offset

HI @dunhamjared,

Renamed temporary column row_num to temp_row_num to help reduce conflicts Removes the temp_row_num column from the results if offset used

Renaming the column definitely makes sense. Not sure if we do it consistently, but we use laravel_ as a prefix for internal columns in other places.

I would say that this is a breaking change and needs to go into the next major release. There could be people that actually use the row_num in their applications. Definitely an edge case, but we can't rule it out completely.

Or should we instead use OFFSET and FETCH instead of the current row_number() implementation?

What's the minimum required SQL Server version for that?

Created at 3 days ago
Composite Keys only in PHP 8?

Hi @KingBier, Composite keys are only available for Laravel 9 at the moment. What version are you using?

What composite keys would you need to define in this case?

Created at 3 days ago
Category has many manufacturers from product brands

Use this relationship:

public function manufacturers() {
    return $this->hasManyDeep(
        Manufacturer::class,
        ['category_product', Product::class, Brand::class],
        [
            'category_id',
            'id',
            'id',
            'id',
        ],
        [
            'id',
            'product_id',
            'brand_id',
            'manufacturer_id',
        ]
    );
}
Created at 3 days ago
I will donated this projects !! you save my day

Send your paypal email here , of course you must replied with your author accounts !

Created at 4 days ago
Getting count with whereDepth

In that case, you can compare the performance to an approach that builds the whole tree once:

$states = Model::tree()->get()->toTree();

foreach ($states as $state) {
    $cities = $state->children->pluck('children')->collapse();
    $villages = $cities->pluck('children')->collapse();

    $count1 = $cities->count();
    $count2 = $villages->count();
}
Created at 4 days ago
Category has many manufacturers from product brands

Hi @Stelikas,

['category_product', Product::class], [Brand::class]

Is this a typo or are passing two separate arrays here? It should be ['category_product', Product::class, Brand::class].

If that's not the issue, please share the migrations (local and foreign keys) of all tables involved in this relationship.

Created at 4 days ago
Using whereHas on self-related realtionships

We already know the condition, we can only qualify the column when it's self related relation and is not already qualified (i.e. it doesn't contain '.') right? so it won't be a breaking change.

That's not possible because we don't know which table an unqualified constraint is referring to. In your case, it could refer to the teams table and we would break the query by automatically qualifying the column.

Created at 5 days ago
How to get User Favorites:

Yes, that's correct.

Created at 5 days ago
Bulder->paginate queries entire tree to get total amount of items

Does the query work for you?

Created at 5 days ago
Getting count with whereDepth

What's the maximum depth in your tree? Do you have a lot of descendants with depth > 4?

Created at 5 days ago
Showing root element when filtering children

Hi @gpluess, You need to group the constraints:

Model::tree()
    ->where(function($query) {
        $query->where('name->' . app()->getLocale(), 'LIKE', '%ing%')
              ->orWhereHas('children', function ($query) {
                  $query->where('name->' . app()->getLocale(), 'LIKE', '%ing%');
              });
    })->orWhere(function ($query) {
         $query->where('subject_id', 1)
               ->orWhereIn('parent_id', function ($query) {
                   $query->select('id')->from('models')->where('subject_id', 1);
               });
     })
    ->get();
Created at 6 days ago
staudenmeir create tag v1.11.4
Created at 1 week ago

Document reverse custom paths

Created at 1 week ago

Remove deprecated Composer option

Support reverse custom paths

Created at 1 week ago
Bulder->paginate queries entire tree to get total amount of items

Use this to only get root models:

$paginator = Model::isRoot()->paginate();

Do you then need the tree for every paginated root model?

Created at 1 week ago
Bulder->paginate queries entire tree to get total amount of items

Hi @Warmic,

I wanted to use "paginate "method for pagination of root models.

What query are you executing here?

Created at 1 week ago
Showing root element when filtering children

Hi @gpluess, Do you mean like this?

Model::where('name->' . app()->getLocale(), 'LIKE', '%ing%')
    ->orWhereHas(
        'children',
        fn($query) => $query->where('name->' . app()->getLocale(), 'LIKE', '%ing%')
    )->get();
Created at 1 week ago
Custom variable to a constraint

Hi @DekaAkademi, Do you mean like this?

$constraints = function ($query) use ($lessons) {
    $query->whereIn('lesson_id', $lessons)->orderBy('order', 'desc);
};

$tree = Subject::treeOf($constraints)->get()->toTree();
Created at 1 week ago
Getting count with whereDepth

You can get the counts in a single query, but – depending on your table size and database engine – that is not guaranteed to be faster than two separate queries:

$descendants = Model::find(2)->descendants()->whereBetween('depth', [3, 4])->where('status', 1)->get(['depth']);

$count1 = $descendants->where('depth', 3)->count();
$count2 = $descendants->where('depth', 4)->count();
Created at 1 week ago
staudenmeir/belongs-to-through v2.5 requires illuminate/database ~5.0

Hi @chibyk101, Please try installing the package with composer require staudenmeir/belongs-to-through instead.

Created at 1 week ago
Automatic path without model ID

Try something like this:

$tree = Model::treeOf($constraint)->depthFirst()->get();

$version = [];
$depth = -1;

foreach ($tree as $model) {
    if ($model->depth > $depth) {
        $version[$model->depth] = 1;
    } else {
        if ($model->depth < $depth) {
            $version = array_slice($version, 0, $model->depth + 1);
        }

        $version[$model->depth]++;
    }

    $depth = $model->depth;

    $model->version_path = implode('.', $version);

    dump($model->version_path);
}
Created at 2 weeks ago
Automatic path without model ID

What do you want to do with that path? Show it in a view?

I looked into generating the path in SQL and it's probably possible, but the query would be pretty complex. It would be much easier to generate the path in PHP after executing the query.

Created at 2 weeks ago
Automatic path without model ID

Which database engine are you using?

Created at 2 weeks ago
Automatic path without model ID

I'll see if there's a solution for that.

Created at 2 weeks ago
Root parent with relationships

I can’t see the relationship when I try to view it from it’s parent (null)?

What query are you executing in this case?

Created at 2 weeks ago
Root parent with relationships

Hi @ottz0, Please share the migrations of all tables that are involved here.

Created at 2 weeks ago
Automatic path without model ID

Hi @maximesahagian, Which relationship are you using? What does your query look like?

Please share a few sample rows and the paths you are getting at the moment vs. the paths you would like to get.

Created at 2 weeks ago