PHP code example of musta20 / laravel-records-filter

1. Go to this page and download the library: Download musta20/laravel-records-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

musta20 / laravel-records-filter example snippets



use Musta20\LaravelRecordsFilter\HasFilter;

class Post extends Model
{
    use HasFactory, HasFilter;





    public function sortFilterOptions()
    {

        return  [
            [
                "lable" => "oldest",    // label name to display in the filtering form
                "type" => 'ASC',        // sorting type
                "filed" => "created_at" // talbel filed name
            ],
            [
                "lable" => "newest",
                "type" => 'DESC',
                "filed" => "created_at"
            ],
            [
                "lable" => "high price",
                "type" => 'DESC',
                "filed" => "price"
            ],
            [
                "lable" => " lowest price",
                "type" => 'ASC',
                "filed" => "price"
            ],

        ];
    }






    public function relationsFilterOptions()
    {
        return [
            [
                'label' => 'auther',     // label name to display in the filtering form
                'label_filed' => 'name', // filed name in the relation tabel
                'id' => 'user_id',      // label name in the relation tabel
                'model' => 'App\Models\User',
            ],
            [
                'label' => 'category',
                'label_filed' => 'name',
                'id' => 'category_id',
                'model' => 'App\Models\category',
            ]
        ];
    }







    public function searchFields()
    {
        return  [
            'title',
            'body'
        ];
    }






    public function filterOptions()
    {

        return [
            [
                'label' => 'reviewing',     // label name to display in the filtering form
                'type' => 'checkbox',       // checkbox | select | radio group | date | between 
                'filed' => 'article_type',  // filed name in the tabel
                'operation' => '=',         // opertaion type
                'options' => [              // the  values you want to filter based on 
                    'reviewed' => 1,        // the first key will be used as label 
                    'not reviewed' => 0
                ],
            ],
            [
                'label' => 'publish status',
                'type' => 'select',    //checkbox | select | radio group | date | between 
                'filed' => 'is_published',
                'options' => [
                    'drafted' => 0,
                    'publish' => 1
                ]

            ],
            [
                'label' => 'font size',
                'type' => 'radio group', //checkbox | select | radio group | date | between 
                'filed' => 'font_type',
                'options' => [
                    'small' => 10,
                    'big' => 20
                ]

            ],
            [
                'label' => 'issue date',
                'type' => 'date', //checkbox | select | radio group | date | between 
                'filed' => 'created_at',
                'operation' => '='

            ]
            ,
            [
                'label' => 'issue period',
                'type' => 'range', //checkbox | select | radio group | date | between 
                'filed' => 'created_at',
                'inputType' => 'date',//date/number
                'operation' => 'between',
                'options' => [
                    'from',
                    'to'
                ]
            ]
            ,
            [
                'label' => 'price range',
                'type' => 'range', //checkbox | select | radio group | date | between 
                'filed' => 'price',
                'inputType' => 'number',//date/number
                'operation' => 'between',
                'options' => [
                    'max',
                    'min'
                ]
            ]
        ];
    }





    $posts= Post::filter();

    // or

    $posts=  Post::where('name','alie')->filter();



{{ $posts->filterLinks() }}

 {{ $posts->filterLinks('laravelRecordsFilter::nav-filter') }}

  {{ $posts->filterLinks('laravelRecordsFilter::sidebar-filter') }}


   {{ $posts->filterNav() }}

bash
php artisan vendor:publish --tag=laravel-Records-Filter