在 Laravel 航海者相关 table 中搜索

Search in Laravel voyager related table

有 2 个 table 用于管理类别,一个用于家长,另一个用于子类别。当我想用这个关联字段进行搜索时,我做不到。并得到这个错误

Error description

  1. Hizmetler 型号(子类别):
    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 模型(父类别):
    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

你的关系不正确。根据您的情况,类别与其child之间的关系是一对多

Parent 型号:

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

Child 型号:

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

您可以在此处阅读文档和示例:https://laravel.com/docs/9.x/eloquent-relationships#one-to-many