-2

There is 2 table for managing categories one for parents and another for sub-categories. When I want to search with this associated field, I can't. and get this error

Error description

  1. Hizmetler Model (Sub-category):
    class Hizmetler extends Model
    {
        protected $primaryKey = 'hizmet_id';
        protected $table = 'hizmetler';
        public $incrementing = false;
    
        use SoftDeletes;
        protected $dates = ['deleted_at'];
        public function Hizmetler(){
            return $this->belongsTo(Hizmetler::class,'hizmet_ust_kategori');
        }
    }
  1. UstKategori Model (Parent category):
    class UstKategori extends Model
    {
        protected $primaryKey = 'id';
        protected $table = 'ust_kategori';
        public $incrementing = false;
    
        use SoftDeletes;
        protected $dates = ['deleted_at'];
        public function UstKategori(){
            return $this->hasOne(UstKategori::class);
        }
    }

Category Table Sub Categroy Table

JrsXcle
  • 3
  • 4

2 Answers2

1

Your relationship is incorrect. according to your case, the relationship between the categories and their child is One to Many.

Parent Model:

class UstKategori extends Model
{
    public function childs(){
        return $this->hasMany(Hizmetler::class, 'hizmet_ust_kategori');
    }
}

Child Model:

class Hizmetler extends Model
{
    public function parent(){
        return $this->belongsTo(UstKategori::class, 'hizmet_ust_kategori');
    }
}

You can read the documentation and examples here: https://laravel.com/docs/9.x/eloquent-relationships#one-to-many

A.Seddighi
  • 1,695
  • 1
  • 20
  • 43
0

I solved this problem by customizing the index method in VoyagerBaseController and copying the method to my controller. The work steps are as follows.

1- Copy the public function index to your controller

2- Replacing search commands. These commands are inside the conditional command if ($search->value != '' && $search->key && $search->filter).

3- Remove the above evil content and replace it with the following content:

        if ($search->value != '' && $search->key && $search->filter) {
            if ($search->key == 'model_id' || $search->key == 'color_id')
                $query->where($search->key, $search->value);
            else if ($search->key == 'image_belongsto_color_relationship') {
                $query->whereHas('color', function ($query) use ($search) {
                    if ($search->filter == 'equals') {
                        $query->where('title', $search->value);
                    } else {
                        $query->where('title', 'LIKE', '%' . $search->value . '%');
                    }
                });
            } 
        }

Note: Don't forget to define the relationship in the desired model. For the example above, it is defined as follows:

class Images extends Model
{
    use HasFactory;
    public function color(): BelongsTo
    {
        return $this->belongsTo(Color::class, 'color_id');
    }
}

don't forget the definition of the relationship in Bread Voyager. My relationship is defined as belongTo type.

gigo
  • 1
  • 1