PHP code example of mihai-valentin / laravel-order-by-field

1. Go to this page and download the library: Download mihai-valentin/laravel-order-by-field 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/ */

    

mihai-valentin / laravel-order-by-field example snippets


use \Illuminate\Support\Facades\DB;

// Order records by a column asc
DB::table('table_name')->orderByField('column', ['first', 'second', 'third']);

// Order records by a column desc
DB::table('table_name')->orderByField('column', ['first', 'second', 'third'], 'desc');
DB::table('table_name')->orderByFieldDesc('column', ['first', 'second', 'third']);

use \Illuminate\Support\Facades\DB;

// Before
DB::table('table_name')->orderByRaw("field(`column`, 'first', 'second', 'third')");

// With macro
DB::table('table_name')->orderByField('column', ['first', 'second', 'third']);

use \Illuminate\Support\Facades\DB;

// Before
DB::table('table_name')->orderByRaw("
    case
        when \"column\"='first' then 1
        when \"column\"='second' then 2
        when \"column\"='third' then 3
        else 0
    end
");

// With macro
DB::table('table_name')->orderByField('column', ['first', 'second', 'third']);



namespace Illuminate\Contracts\Database\Query {
    use MihaiValentin\LaravelOrderByFiled\OrderByFieldServiceProvider;

    /**
     * @method Builder orderByField(string $column, array $order, string $direction = 'asc')
     * @method Builder orderByFieldDesc(string $column, array $order)
     *
     * @see OrderByFieldServiceProvider
     */
    interface Builder {}
}

namespace Illuminate\Database\Eloquent {
    /**
     * @method Builder orderByField(string $column, array $order, string $direction = 'asc')
     * @method Builder orderByFieldDesc(string $column, array $order)
     *
     * @see OrderByFieldServiceProvider
     */
    class Builder implements BuilderContract {}
}