在Laravel Voyager相关表中搜索
有2个用于管理父母的类别的表,另一个用于子类别。当我想使用此关联的字段搜索时,我不能。并获取此错误
- Hizmetler模型( 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');
}
}
- ustkategori模型( parent类别):
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);
}
}
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
- 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');
}
}
- 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的关系不正确。根据您的案例,类别与孩子之间的关系是一对。
父型:
孩子模型:
您可以在此处阅读文档和示例: https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
Your relationship is incorrect. according to your case, the relationship between the categories and their child is One to Many.
Parent Model:
Child Model:
You can read the documentation and examples here: https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
我通过在Voyagerbasecontroller中自定义索引方法并将该方法复制到我的控制器来解决此问题。
工作步骤如下。
1-将公共功能索引复制到您的控制器
2-替换搜索命令。这些命令在
条件命令if($ search-> value!=''''&& $ search- key&&& $ search-> filter)。
3-删除上述邪恶内容,然后用以下内容替换它:
注意:不要忘记在所需模型中定义关系。对于上面的示例,它定义为以下内容:
不要忘记面包旅行者中关系的定义。我的关系定义为属于类型。
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:
Note: Don't forget to define the relationship in the desired model. For the example above, it is defined as follows:
don't forget the definition of the relationship in Bread Voyager. My relationship is defined as belongTo type.
现在,替换表名“下载” 在您自己的表中。您可以从 yourwebsite.com/admin/database中获得。
在哪里添加: -
转到您的项目目录“供应商/tcg/voyager/resources/views/views/bread/browse.blade.php。fyi
:请确保您在表HREF中使用自己的表替换在下面的表HREF中使用您自己的表。
如果您不适合想编辑此全局模板,然后将其移至资源/供应商/Voyager/yourbreadname/browse.blade.php
Now Just replace the table name "downloads" to your own table. Which you can get from yourwebsite.com/admin/database.
Where to Add:-
Go to your project directory "vendor/tcg/voyager/resources/views/bread/ browse.blade.php.
FYI: Please make sure you replace the "download" in below table href with your own table to work.
If you don't want to edit this global template then move it to resources/vendor/voyager/yourbreadname/browse.blade.php