Laravel 使用加入的 Nova 过滤器 table
Laravel Nova Filter using joined table
我正在尝试根据来自两个不同表的数据为我的模型“问题”构建一个 Laravel Nova 过滤器。我已经弄清楚我需要使用的确切 SQL 查询,但无法弄清楚如何在 Nova 过滤器 .[=16 的 apply() 方法中使用它=]
Table“问题”栏目:
id, ...
Table“主题”栏目:
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
查看代码中的注释
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
class QuestionsThemeFilter extends BooleanFilter
{
public function apply(Request $request, $query, $value) {
// Since you are using BooleanFilter $value will be an associate array with true/false value
$selectedThemes = Arr::where($value, function ($v, $k) {
return $v === true;
});
$selectedThemeIds = array_keys($selectedThemes)
// For debugging print some logs and check
Log::info($value)
Log::info($selectedThemeIds);
return $query->join('themes', 'questions.id', '=', 'themes.question_id')
->whereIn('themes.theme_id', $selectedThemeIds);
// If you are using normal filter then below code will be enough
/*
return $query->join('themes', 'questions.id', '=', 'themes.question_id')
->where('themes.theme_id', $value);
*/
}
public function options(Request $request) {
// You can use Laravel collection method pluck
return Themes::pluck('id', 'title')->all();
}
}
参考文献:
我正在尝试根据来自两个不同表的数据为我的模型“问题”构建一个 Laravel Nova 过滤器。我已经弄清楚我需要使用的确切 SQL 查询,但无法弄清楚如何在 Nova 过滤器 .[=16 的 apply() 方法中使用它=]
Table“问题”栏目:
Table“主题”栏目:
我正在尝试使用的 SQL 查询: 我在 Laravel Nova 中的过滤器 QuestionsThemeFilter.php: 那么,我应该怎么做呢? 其他问题: 由于我是Laravel的初学者,我应该如何开始调试呢?使用 dd() 不起作用(过滤器刚刚中断),我不知道如何调试它。有没有办法从 apply 方法中转储一些值(例如 $value)? 在此先感谢您的帮助,非常感谢:-Did, ...
id, question_id, theme_id
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)
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;
}
}
查看代码中的注释
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
class QuestionsThemeFilter extends BooleanFilter
{
public function apply(Request $request, $query, $value) {
// Since you are using BooleanFilter $value will be an associate array with true/false value
$selectedThemes = Arr::where($value, function ($v, $k) {
return $v === true;
});
$selectedThemeIds = array_keys($selectedThemes)
// For debugging print some logs and check
Log::info($value)
Log::info($selectedThemeIds);
return $query->join('themes', 'questions.id', '=', 'themes.question_id')
->whereIn('themes.theme_id', $selectedThemeIds);
// If you are using normal filter then below code will be enough
/*
return $query->join('themes', 'questions.id', '=', 'themes.question_id')
->where('themes.theme_id', $value);
*/
}
public function options(Request $request) {
// You can use Laravel collection method pluck
return Themes::pluck('id', 'title')->all();
}
}
参考文献: