1. Go to this page and download the library: Download s-damian/larasort 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/ */
s-damian / larasort example snippets
private array $sortables = [ // The attributes that are sortable.
'email',
'first_name',
'created_at',
];
$customers = Customer::whereNotNull('confirmed_at')
->autosort() // Automate ORDER BY and its direction.
->paginate();
use SDamian\Larasort\AutoSortable;
class Customer extends Model
{
use AutoSortable;
/**
* The attributes that are sortable.
*/
private array $sortables = [
'id', // "id" column will be the default column for the ORDER BY.
'first_name',
'email',
'created_at',
];
}
use SDamian\Larasort\Larasort;
Larasort::setDefaultSortable('email') // "email" column will be the default column for the ORDER BY.
use SDamian\Larasort\AutoSortable;
class Customer extends Model
{
use AutoSortable;
/**
* The attributes that are sortable.
*/
private array $sortables = [
null, // Will be null by default (by default there will be no ORDER BY).
'id',
'first_name',
'email',
'created_at',
];
}
use App\Models\Customer;
class CustomerController extends Controller
{
public function index()
{
$customers = Customer::whereNotNull('confirmed_at')
->autosort() // Automate ORDER BY and its direction.
->paginate();
return view('customer.index', [
'customers' => $customers,
]);
}
}
use SDamian\Larasort\AutoSortable;
class Customer extends Model
{
use AutoSortable;
/**
* The attributes that are sortable.
*/
private array $sortables = [
'id',
'first_name',
'email',
'created_at',
];
/**
* The attributes that are sortable without table in prefix.
*/
private array $sortablesAs = [
'article_title', // Here.
];
}
class CustomerController extends Controller
{
public function index()
{
$customers = Customer::select([
'customers.*',
'articles.title AS article_title', // Here.
])
->join(
'articles',
'customers.id', '=', 'articles.customer_id'
)
->autosort() // Automate ORDER BY and its direction.
->paginate();
return view('customer.index', [
'customers' => $customers,
]);
}
}
/**
* The attributes of its sortable relations.
*/
private array $sortablesRelated = [
// Convention: {relationship name}{separator}{column in this relationship table}.
'article.title',
];
public function article()
{
return $this->hasOne(Article::class, 'user_id_created_at', 'id');
}
$users = User::autosortWith('article', [
'join_type' => 'join', // Optional - "leftJoin" by default.
'columns' => ['id', 'username', 'email', 'role'], // Optional - "*" by default.
'related_columns' => ['title AS article_title', 'h1'], // Optional - "*" by default.
])
->paginate();
/**
* The attributes of its sortable relations.
*/
private array $sortablesRelated = [
// Convention: {relationship name}{separator}{column in this relationship table}.
'articles.title',
];
public function articles()
{
return $this->hasMany(Article::class, 'user_id_created_at', 'id');
}
$users = User::autosortWith('articles', [
'join_type' => 'join', // Optional - "leftJoin" by default.
'columns' => ['id', 'username', 'email', 'role'], // Optional - "*" by default.
'related_columns' => ['title AS article_title', 'h1'], // Optional - "*" by default.
])
->paginate();
private array $sortablesRelated = [
// Convention: {relationship name}{separator}{column in this relationship table}.
'user.email',
];
public function user()
{
return $this->belongsTo(User::class, 'user_id_created_at', 'id');
}
$articles = Article::autosortWith('user', [
'join_type' => 'join', // Optional - "leftJoin" by default.
'columns' => ['id', 'slug', 'h1', 'updated_at'], // Optional - "*" by default.
'related_columns' => ['email AS user_email', 'first_name'], // Optional - "*" by default.
])
->paginate();
use SDamian\Larasort\AutoSortable;
class Article extends Model
{
use AutoSortable;
/**
* The attributes that are sortable.
*/
private array $sortables = [
'id',
'title',
'updated_at',
];
/**
* The sortable attributes to which their table is specified.
*/
private array $sortablesToTables = [
'id' => 'categories.id', // Here.
];
}
use SDamian\Larasort\Larasort;
class ArticleController extends Controller
{
public function index()
{
Larasort::setSortablesToTables(['id' => 'categories.id']); // Here.
// Here your SQL query with ->autosort()
// Then the rest of the code...
}
}
use SDamian\Larasort\Larasort;
class InvoiceController extends Controller
{
public function index()
{
Larasort::setSortablesDefaultOrder([
'desc' => ['id', 'created_at', 'price_with_vat'], // Here.
]);
// Here your SQL query with ->autosort()
// Then the rest of the code...
}
}
use SDamian\Larasort\Larasort;
class InvoiceController extends Controller
{
public function index()
{
Larasort::setSortablesDefaultOrder([
'asc' => ['customer_email', 'customer_first_name'], // Here.
]);
// Here your SQL query with ->autosort()
// Then the rest of the code...
}
}
use SDamian\Larasort\Manual\LarasortManual;
class CustomerController extends Controller
{
public function index()
{
$larasortMan = new LarasortManual();
$larasortMan->setSortables(['id', 'first_name', 'email', 'created_at']); // Here.
$resultLarasortMan = $larasortMan->get();
$customers = DB::select('
SELECT *
FROM customers
ORDER BY '.$resultLarasortMan['order_by'].' '.$resultLarasortMan['order'].'
');
return view('customer.index', [
'customers' => $customers,
'larasortManAttrs' => $resultLarasortMan['attrs'],
]);
}
}
use SDamian\Larasort\Manual\LarasortManual;
class ArticleController extends Controller
{
public function index()
{
$larasortMan = new LarasortManual();
$larasortMan->setSortables(['id', 'title', 'created_at']);
$larasortMan->setSortablesToTables(['id' => 'categories.id']); // Here.
$resultLarasortMan = $larasortMan->get();
// Here your SQL query with $resultLarasortMan['order_by'] and $resultLarasortMan['order']
// Then the rest of the code...
}
}
use SDamian\Larasort\Manual\LarasortManual;
class InvoiceController extends Controller
{
public function index()
{
$larasortMan = new LarasortManual();
$larasortMan->setSortables(['id', 'ref', 'customer_email', 'created_at', 'price_with_vat']);
$larasortMan->setSortablesDefaultOrder([
'desc' => ['id', 'created_at', 'price_with_vat'], // Here.
]);
$resultLarasortMan = $larasortMan->get();
// Here your SQL query with $resultLarasortMan['order_by'] and $resultLarasortMan['order']
// Then the rest of the code...
}
}