fomvasss/laravel-url-facet-filter

构建和显示分面过滤参数以及管理它们

1.7.0 2024-09-26 10:26 UTC

This package is auto-updated.

Last update: 2024-09-26 10:26:57 UTC


README

License Build Status Latest Stable Version Total Downloads Quality Score

构建和显示分面过滤参数以及管理它们

安装

从命令行运行

composer require fomvasss/laravel-url-facet-filter

发布

php artisan vendor:publish --provider="Fomvasss\UrlFacetFilter\ServiceProvider"

用法

构建 URL

<a href="{{ \FacetFilter::build('color', 'red') }}">Red</a>	
<a href="{{ \FacetFilter::build('color', 'green') }}">Green</a>	
<a href="{{ \FacetFilter::build('size', 'm') }}">M</a>	
<a href="{{ \FacetFilter::build('size', 'xl') }}">XL</a>	

在控制器中的用法

app/Http/Controllers/ProductController.php

<?php 

namespace App\Http\Controllers;

class ProductController extends Controller 
{
        public function index(Request $request)
        {
            $filterAttrs = \FacetFilter::toArray($request->get(\FacetFilter::getFilterUrlKey()));
            
            $products = Product::with('media')
                ->facetFilterable($filterAttrs) // facetFilterable - for example your scope
                ->get();

            return view('article.index', [
                'articles' => $products,
            ]);
        }  
}

示例,在 URL 字符串中

https://my-site.com/products?⛃=color☛red⚬green♦size☛m♦ergonomic☛form-1⚬form-2

在 PHP 控制器中(在 prepare FacetFilter::toArray() 后)

array:3 [▼
  "color" => array:2 [▼
    0 => "red"
    1 => "green"
  ]
  "size" => array:1 [▼
    0 => "m"
  ]
  "ergonomic" => array:2 [▼
    0 => "form-1"
    1 => "form-2"
  ]
]

链接