2017-02-09 56 views
1

我正在使用OctoberCMS,根據LaravelTwig作出一個簡單的畫廊。如何在PHP Laravel Twig中按類別篩選記錄?

如何按類別過濾記錄?

以下就是我正在做的現在,但我不認爲這是一個很好的解決方案:

我篩選HTML,但完整的記錄仍然存在的類外的所有項目,引起額外的分頁和空白頁面。

數據庫

  • 表 '畫廊' 持有的類別名稱的列表。

  • 表'圖像'包含圖像名稱及其標記的類別。

圖像列表

的URL參數是/畫廊/:類/?頁?

訪問像/ gallery/nature/1這樣的網址將使用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 %} 

記錄

結果 '記錄' 和 '記錄'。

for循環在'images→category_tag'記錄中顯示'gallery→category'記錄。

分頁顯示所有'圖像→名稱'記錄。

{% 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') 

解決方法?

有沒有一種方法可以在生成html列表之前按類別過濾'記錄'?

+1

不知道靈感,如果我誤解了,但是,是不是簡單的將它傳遞給之前獲取符合條件的記錄風景?像'Gallery :: where('category_id',$ catId) - > get()'。當然,您首先必須使用從參數 –

+0

@ChristopherFrancisco獲取的名稱獲取類別ID。通過OctoberCMS,您可以拖放記錄列表或記錄詳細信息,然後設置模型類和顯示列。從那裏你可以使用Twig語法來使用記錄。我是新手,在Twig之外使用Laravel。 –

+0

我的不好,OctoberCMS的鏈接重定向到分頁頁面,所以我認爲它只是一個分頁組件(儘管名稱說CMS,愚蠢的我哈哈哈) –

回答

2

這是我會怎麼處理它:

  • 創建你的圖像模式的涵蓋範圍由頁,分類ECT過濾器..
  • 在處理你的組件URL特性和邏輯無法查看

比方說圖庫組件:

URL: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; 

} 
在你的圖片型號

// 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); 
    } 

答案從RainLab Blog Plugin