1

I have estate_object table which holds information about estate, for example: address, city, region etc. Then i have a seperate tables which holds just specific property of an object, for example on of the tables is objects_near. I made seperate tables for that because each of those models can hold multiple values(they are checkboxes)for example objects_near cant hold values like - parking, airport, see, store etc. So to escape unnecessary columns and null fields in estate_object table i made polymorphic many to many relationship between estate_objects table and tables which hold properties. In my view i want to make a filter and querying things from estate_object table works fine. To filter properties which belong to one or more estate objects i have checkboxes. For example to query objects near estate i have checkboxes with multiple options. I can't figure out and can't find solution as well on how to query objects_near properties which are related just to specific estate_object.

This is how my checkboxe looks like:

<div class="form-group align-items-center">
  <label class="col-form-label text-md-right">{{ trans('admin.estate-obj.label.columns.objects_near') }} 
  </label><br>
     @foreach($estate_objects_near as $objects_near)
         <input type="checkbox" id="{{ $objects_near->name }}" name="objects_near[]" value="{{ 
          $objects_near->id }}">  
       <label for="{{ $objects_near->name }}">{{ $objects_near->name }} </label>
     @endforeach  
/div>

Estate_object_near model:

  public function estateObj()
    {
        return $this->morphToMany(EstateObj::class, 'estateable');
    }

EstateObj model:

   public function objectsNear()
    {
        return $this->morphedByMany(Estate_objects_near::class, 'estateable');
    }

EstateObjController:

   function( $query) use ($request) {

                if($request->has('address')){
                    $query->where('address', $request->address);
                }

                if($request->has('price')){                      
                    $query->whereBetween('price', [$request->price['min'], $request->price['max']]);
                }

                if($request->has('room_qty') ){
                    $query->where('room_qty', $request->room_qty);
                }

                // trying to access properties which are selected in checkbox and related to estate 
                 object
               if($request->has('objects_near')){

                $objNear = Estate_objects_near::find(1);
                $obj = $objNear->estateObj;
                $query->where($obj, $request->objects_near);
            }

             
            });

If i dump out $obj i am getting relations with pivot table 'estateable' and i can see which property is related with specific estate obj, but when i try to execute it shows me SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters. I have tried to use whereHas function as well, but then i get this error - Argument 1 passed to Illuminate\Database\Eloquent must be an instance.

Have tried whereHasMorph as well:

$query->whereHasMorph('estateable', [Estate_objects_near::class], function ($query) {
     $query->where('id', $request->objects_near);
     })->get();
                

This leads to 'Call to undefined method App\Models\EstateObj::estateable()'

Would appreciate suggestions very much.

1 Answers1

0

Following query worked for me:

         if($request->has('objects_near')){
            $query->whereHas('objectsNear', function ($query) use ($request) {
            $query->whereIn('estate_objects_near.id', [$request->get('objects_near')]);
            })->get();
        }

As it turned out whereHasMorph works just with morphTo relations. If anybody has the same problem and would like to know more of my code structure let me know.