abbasudo / laravel-purity

An elegant way to filter and sort queries in Laravel
https://abbasudo.github.io/laravel-purity/?utm_source=github&utm_medium=about
MIT License
429 stars 38 forks source link

filter in many to many relationShip #62

Closed hosseinjarrahi closed 1 month ago

hosseinjarrahi commented 2 months ago

hi bro, i have a many to many RelationShip

Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->timestamps();
});

Schema::create('categorizables', function (Blueprint $table) {
      $table->unsignedBigInteger('category_id');
      $table->unsignedBigInteger('categorizable_id');
      $table->string('categorizable_type');
});

Schema::create('contents', function (Blueprint $table) {
    $table->id();
    $table->string('title');
});

this is my filter string domain.com/route?filters[categories][id][$eq]=1

but it Does not work

abbasudo commented 2 months ago

Hey @hosseinjarrahi. Laravel Purity looks for relation inside your model and have nothing to do with your schema. Define relation in your contents model.

// Content.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Content extends Model
{
    /**
     * Get all of the categories that this content belongs to.
     */
    public function categories()
    {
        return $this->morphToMany(Category::class, 'categorizable');
    }
}