mehdi-fathi/eloquent-filter

WhereIn request for relationships

uzkurama opened this issue · 10 comments

I have relation like ' order_details[markets][market_id] ' , and making form get request with select2 multiple parameter.

How to pass the array to request, now I'm getting error like

Call to undefined method App\Models\Markets:market_id()

when I add [] in the last of name attribute like order_details[markets][market_id][], same with order_details[][markets][market_id]

P.S. Thanks for the extension, it's pretty good and understandable than spatie-query-builder. Good job man

I forgot to provide my URL request, here is

orders?order_details[markets][market_id][]=1&order_details[markets][market_id][]=3

Tried custom filter, when processing detect() function, when I check $field variable, get the first key of the array, not as an array (order_details.markets.market_id.0), $param get just one value too

That's why I can't use IN condition in the query.

Maybe I do smt wrong, don't know. Well, will wait for your answer & help too. Thanks in advance

@uzkurama Hi
maybe you made a mistake in your model check it again.if you can't fix it.past your code here to review that.

Orders model

class Orders extends Model
{
    use HasFactory;

    private static $whiteListFilter = ['*'];

    public function order_details(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(OrderDetails::class);
    }

OrderDetails model

class OrderDetails extends Model
{
    use HasFactory;

    public function orders(): BelongsTo
    {
        return $this->belongsTo(Orders::class);
    }

    public function inventories(): BelongsToMany
    {
        return $this->belongsToMany(Inventory::class, 'orders_inventories', 'order_details_id', 'inventory_id');
    }

    public function markets(): BelongsToMany
    {
        return $this->belongsToMany(Markets::class, 'orders_markets', 'order_details_id', 'market_id');
    }
}

that's all with models & and give you part of my view code.

@foreach($markets as $key => $m) {{ $m->market_name }} @Endforeach

and request orders?order_details[markets][market_id][]=1&order_details[markets][market_id][]=3

P.S. I removed the custom filter because today we needed workly code

@uzkurama where is use Filterable;?

@uzkurama where is use Filterable;?

It is also there, I probably forgot to copy it, since there is a lot of code in the model that does not touch upon this topic.

@uzkurama where is use Filterable;?

any news or updates?

@uzkurama We have many unit tests for every situation. also many users using the eloquent filter. we don't have a problem in wherein the query or custom method. if you are sure that there is a problem, you can fix it and merge it.

@uzkurama We have many unit tests for every situation. also many users using the eloquent filter. we don't have a problem in wherein the query or custom method. if you are sure that there is a problem, you can fix it and merge it.
@mehdi-fathi ok, when I have free time, I will try to fix it. But anyway thanks for your attention. I'm closing the issue

Well, I found the solution. I made a custom query filter trait for some attributes (in my case is 'market_id')

And it works!

Anyway, thank you @mehdi-fathi for your documentation!

Attaching my code there for other extension users.

use Illuminate\Database\Eloquent\Builder;

/**
 * Trait usersFilter.
 */
trait MarketFilter
{
    /**
     * @param Builder $builder
     * @param $value
     *
     * @return Builder
     */
    public function market_ids(Builder $builder, $value)
    {
        return $builder->whereHas('order_details', function ($q) use ($value){
            $q->whereHas('markets', function ($k) use ($value){
                $k->whereIn('market_id', $value);
            });
        });
    }
}