【发布时间】:2021-05-25 04:37:00
【问题描述】:
对不起,我的英语不好。这是我在这里的第一篇文章,我是开发中的菜鸟..
感谢大量阅读和一些教程,我设法构建了一个小型 Laravel 应用程序。
出于各种原因,我决定加入 Livewire,这很棒。
但是,我在搜索和过滤来自不同模型和关系的条目时遇到了一些问题。
Image of what I'm trying to do
我有一个名为 Seance 的模型,我可以搜索等级、主题和产品模型(图片上的 2、3 和 5)。
我的降神会模型:
class Seance extends Model
{ 使用软删除;
protected $guarded = [];
public function product()
{
return $this->belongsTo('App\Product');
}
public function theme()
{
return $this->belongsTo('App\Theme');
}
public function specialty()
{
return $this->belongsTo('App\Specialty');
}
public function grade()
{
return $this->belongsTo('App\Grade');
}
public function courses()
{
return $this->hasMany('App\Course');
}
public function category()
{
return $this->belongsTo('App\Category')->withTrashed();
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function specialties(): BelongsToMany
{
return $this->belongsToMany('App\Specialty', 'seance_specialty', 'seance_id', 'specialty_id');
}
public function scopeSearch($query, $term)
{
$term = "%$term%";
$query->where(function($query) use ($term){
$query->where('id', 'like', $term)
->orWhere('name', 'like', $term)
->orWhere('slug', 'like', $term)
->orWhere('subtitle', 'like', $term)
->orWhere('description', 'like', $term)
->orWhereHas('grade', function($query) use ($term){
$query->where('name', 'like', $term);
})
->orWhereHas('product', function($query) use ($term){
$query->where('name', 'like', $term);
})
->orWhereHas('theme', function($query) use ($term){
$query->where('name', 'like', $term);
})
->orWhereHas('category', function($query) use ($term){
$query->where('name', 'like', $term);
});
});
}
}
但我想搜索与 3 相关的第 4 点和与 5 相关的第 6 点,但我不知道该怎么做。
第 3 点是一个名为 Theme 的模型,它属于一个名为 Discipline 的模型(第 4 点)
public function discipline()
{
return $this->belongsTo('App\Discipline');
}
第 5 点是一个名为 Produt 的模型,它属于一个名为 Coursetype 的模型(第 6 点)
public function coursetype()
{
return $this->belongsTo('App\Coursetype');
}
我知道在某处可以加入,但我不知道在哪里...
这是我的 Livewire 组件:
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search = "";
public $paginate = 5;
public $selectedGrade = null;
public $selectedProduct = null;
public $selectedCategory = null;
public $selectedTheme = null;
public $selectedDiscipline = null;
public function render()
{
return view('livewire.seances-table', [
'seances' => Seance::with('product', 'theme', 'specialty', 'grade', 'courses', 'category', 'specialties')
->when($this->selectedGrade, function($query){
$query->where('grade_id', $this->selectedGrade);
})
->when($this->selectedProduct, function($query){
$query->where('product_id', $this->selectedProduct);
})
->when($this->selectedCategory, function($query){
$query->where('category_id', $this->selectedCategory);
})
->when($this->selectedTheme, function($query){
$query->where('theme_id', $this->selectedTheme);
})
->when($this->selectedDiscipline, function($query){
$query->where('discipline_id', $this->selectedDiscipline);
})
->search(trim($this->search))
->paginate($this->paginate),
'grades' => Grade::all(),
'products' => Product::all(),
'categories' => Category::all(),
'themes' => Theme::all(),
'disciplines' => Discipline::all()
]);
}
另外,我想筛选学科(第 4 点),但出现此错误。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'discipline_id' in 'where clause'
我知道为什么会收到此错误,但我不知道如何解决。
对不起,如果这一切不是很清楚。
任何帮助将不胜感激。
谢谢大家,来自法国的欢呼
【问题讨论】:
标签: php laravel eloquent laravel-livewire