php - Laravel queries (function find() ) in Laravel throught using polymorphic ralationship

I'm quite new in Laravel and have some question about Laravel SQL query through using polymorphic relationship

The situation is: I have two different tables in my SQL. One table 'estates' contains a list of estates and their parameters Another table is 'type_categories' which contains a list of type categories Every estate can be related to many type categories and every type category can have many estates (so I use polymorphic relationships many to many). They are related through the third table ''

So, as a result, I need to do a query, which will find all estates which is related to category, but my user can choose a numerous of category, not just one.

I tried to use function find(1)->estates; which as a parameter have an id of type category, which related estates I tried to find. It works fine, but the problem is, that my user needs to find an estate to several categories and function, but function finds works with just one parameter in the situation we nave polymorphic relation, not with the several parameters as I need.

Is there some way to use several parameters in function find() and also use polymorphism? Sure, that there exist some other functions for it...

Then I show you my code.

Estates model looks:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Estate extends Model
{
    protected $fillable = ['title', 'slug', 'estate_text', 'created_by', 'modified_by'];

    public function categories() {
        return $this->morphToMany('App\TypeCategory', 'categoryable');
    }
}

Type categories model looks:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class TypeCategory extends Model
{
    protected $fillable = ['title', 'slug', 'parent_id', 'created_by', 'modified_by'];

    public function estates() {
        return $this->morphedByMany('App\Estate', 'categoryable');
    }

}

And finally, estates controller where I tried to select estates model looks:

use Illuminate\Http\Request;
use App\Estate;
use App\TypeCategory;

class EstateController extends Controller {

    public function searchEstate() {

        $estates = TypeCategory::find(1)->estates;

        return view('main', [
            'estates' => $estates
        ] );
    }

}

Answer

Solution:

I don't sure it is your problem or not. try this:

 $estates = Estate::whereHas('categories', function ($query) {
    $query->whereIn('slug', ['xxx','yyy','zzz']);
 })->get();

Source