【发布时间】:2021-06-18 11:00:09
【问题描述】:
我正在尝试根据来自两个不同表的数据为我的模型“问题”构建一个 Laravel Nova 过滤器。我已经弄清楚了我需要使用的确切 SQL 查询,但无法弄清楚如何在 Nova 过滤器的 apply() 方法中使用它。
表“问题”列:
id, ...
表格“主题”列:
id, question_id, theme_id
我正在尝试使用的 SQL 查询:
SELECT questions.*
From questions
Inner JOIN themes on questions.id=themes.question_id
WHERE themes.theme_id = 3 (value taken from the Nova Filter options)
我在 Laravel Nova QuestionsThemeFilter.php 中的过滤器:
class QuestionsThemeFilter extends BooleanFilter
{
public $name = 'filter by theme';
public function apply(Request $request, $query, $value) {
// This is what I'm missing...
}
public function options(Request $request) {
$themes = Themes::all();
$themesArray = array();
foreach($themes as $item) {
$themesArray[$item['title']] = strval($item['id']);
}
return $themesArray;
}
}
那么,我该怎么做呢?
其他问题:由于我是 Laravel 的初学者,我应该如何开始调试呢?使用 dd() 不起作用(过滤器刚刚中断),我不知道如何调试它。有没有办法从 apply 方法中转储一些值(例如 $value)?
提前感谢您的帮助,非常感谢:-D
【问题讨论】:
标签: php laravel laravel-nova