[英]How to filter Records by category in PHP Laravel Twig?
I'm making a simple gallery using OctoberCMS , based on Laravel and Twig . 我正在使用基于Laravel和Twig的 OctoberCMS创建一个简单的画廊。
How can I filter records by category? 如何按类别过滤记录?
Here is how I'm doing it now, but I don't think this is a good solution: 这是我现在的做法,但我认为这不是一个好的解决方案:
I filter the html, but the full records still exists for all the items outside the category, causing extra pagination and blank pages. 我过滤了html,但是该类别以外的所有项目的完整记录仍然存在,从而导致了额外的分页和空白页。
Database 数据库
A Table 'gallery' holds list of category names. 表“图库”包含类别名称列表。
A Table 'images' holds the image names and their tagged categories. 表“图像”包含图像名称及其标记的类别。
Image List 图片清单
The URL parameters are /gallery/:category?/:page? URL参数是/ gallery /:category?/:page?
Visiting a url like /gallery/nature/1 will filter images by category using a for loop. 访问类似/ gallery / nature / 1之类的url将使用for循环按类别过滤图像。
<!-- Gallery Category Record Variable -->
{% set category = record.category %}
<!-- Image List -->
{% for record in records %}
<!-- If Image Category Tag matches Gallery Category Name -->
{% if record.category_tag == category %}
<img src="/{{ record.name }}.jpg">
{% endif %}
{% endfor %}
Records 记录
Results for 'records' and 'record'. “记录”和“记录”的结果。
The for loop is showing 'gallery→category' record in 'images→category_tag' records. for循环在“图像→类别_标签”记录中显示“图库→类别”记录。
The pagination is showing all 'images→name' records. 分页显示所有“图像→名称”记录。
{% for record in records %} = (All in 'images')
{{ records }} = {<ul><li>...<a href="gallery/nature?page=2">2</a>} (All in 'images')
{{ record }} = {"category":"nature","id":7} (Current category in 'gallery')
Solution? 解?
Is there a way to filter the 'records' by category before generating the html list? 有没有一种方法可以在生成html列表之前按类别过滤“记录”?
This is how I would approach it : 这就是我的处理方式:
Let's say Gallery Component : 假设图库组件:
URL : page.com/:cat/:page? 网址:page.com/:cat /:page?
public function defineProperties()
{
return [
// Page # for pagination..
'pageNumber' => [
'title' => 'Page #',
'description' => 'Gallery Page #',
'type' => 'string',
'default' => '{{ :page }}',
],
// Category slug
'category' => [
'title' => 'Category',
'description' => 'Gallery Cat',
'type' => 'string',
'default' => '{{ :cat }}',
],
// Images to show per page
'perPage' => [
'title' => 'Images per page',
'type' => 'string',
'validationPattern' => '^[0-9]+$', // validation
'validationMessage' => 'VValidation Error',
'default' => '15',
],
// if you want to add sorting
'sortOrder' => [
'title' => 'Sort Order',
'description' => 'Images Sort Order',
'type' => 'dropdown',
'default' => 'updated_at desc'
],
];
}
public function getSortOrderOptions()
{
return Image::$allowedSortingOptions;
}
public function init()
{
$this->pageNumber = empty($this->property('pageNumber')) ? 1 : $this->property('pageNumber');
$this->perPage = $this->property('perPage');
$this->sortOrder = $this->property('sortOrder');
$this->category = $this->property('category');
}
public function onRun()
{
// here you may want to do some checks
// and add logic before querying your DB
return $this->listImages($this->pageNumber , $this->sortOrder, $this->perPage, $this->category);
}
public function listImages($pageNumber, $sortOrder, $perPage, $category){
// this is the scope you will define in your Images Model
// to handle pagination, sorting, category filtering ect..
$images = Images::listFrontEnd([
'page' => $pageNumber,
'sort' => $sortOrder,
'perPage' => $perPage,
'category' => $category,
]);
// small helper if the pagination # is > than last page
// redirect to last page..
$lastPage = $images->lastPage();
if ($this->pageNumber > $lastPage && $this->pageNumber > 1){
return Redirect::to($this->currentPageUrl(["page" => $lastPage]));
}
$this->images = $this->page['images'] = $images;
}
in your Image Model : 在您的图像模型中:
// list the allowed sorting options that will show up in your component
public static $allowedSortingOptions = array(
'created_at asc' => 'Images Created (ascending)',
'created_at desc' => 'Images Created (descending)',
'updated_at asc' => 'Images Updated (ascending)',
// ect....
);
// Scope for your component
public function scopelistImages($query, $options)
{
/*
* Default options
*/
extract(array_merge([
'page' => 1,
'perPage' => 15,
'sort' => 'updated_at',
'category' => null
], $options));
// SORTING
if (!is_array($sort)) {
$sort = [$sort];
}
foreach ($sort as $_sort) {
if (in_array($_sort, array_keys(self::$allowedSortingOptions))) {
$parts = explode(' ', $_sort);
if (count($parts) < 2) {
array_push($parts, 'desc');
}
list($sortField, $sortDirection) = $parts;
if ($sortField == 'random') {
$sortField = Db::raw('RAND()');
}
$query->orderBy($sortField, $sortDirection);
}
}
// Filter by category
........
return $query->paginate($perPage, $page);
}
Answer inspired from the RainLab Blog Plugin 灵感源自RainLab Blog插件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.