diff --git a/app/Http/Controllers/ExercicioController.php b/app/Http/Controllers/ExercicioController.php index 57977c7..1197e95 100644 --- a/app/Http/Controllers/ExercicioController.php +++ b/app/Http/Controllers/ExercicioController.php @@ -31,7 +31,7 @@ class ExercicioController extends Controller public function index() { $topicos = Topico::orderBy('order'); - $semTopico = Exercicio::whereDoesntHave('topico')->orderBy('name'); + $semTopico = Exercicio::whereDoesntHave('topico'); /** @var \App\Models\User */ $user = Auth::user(); if (optional($user)->isAdmin()) { @@ -55,7 +55,7 @@ public function create() $this->authorize('create', Exercicio::class); return View('exercicio.create') ->with('pacotesR', $this->getInstalledPackages()) - ->with('topicos', Topico::orderBy('order')); + ->with('topicos', Topico::all()); } /** diff --git a/app/Models/Exercicio.php b/app/Models/Exercicio.php index abb5912..fe96fb3 100644 --- a/app/Models/Exercicio.php +++ b/app/Models/Exercicio.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Stephenjude\DefaultModelSorting\Traits\DefaultOrderBy; use Symfony\Component\Yaml\Yaml as Yaml; class Exercicio extends Model @@ -25,6 +26,12 @@ class Exercicio extends Model 'id' ]; + /** + * Set a default ordering for this model + */ + use DefaultOrderBy; + protected static $orderByColumn = 'name'; + /** * Scope a query to only include active users. * diff --git a/app/Models/Prazo.php b/app/Models/Prazo.php index 17db435..9994b3b 100644 --- a/app/Models/Prazo.php +++ b/app/Models/Prazo.php @@ -5,13 +5,18 @@ use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Support\Carbon; +use Stephenjude\DefaultModelSorting\Traits\DefaultOrderBy; class Prazo extends Pivot { use HasFactory; protected $table = 'prazos'; - protected $guarded = []; + /** + * Set a default ordering for this model + */ + use DefaultOrderBy; + protected static $orderByColumn = 'prazo'; // relationships public function turma() @@ -24,13 +29,13 @@ public function exercicio() } // passado/futuro - public function getPassadoAttribute() + public function getPassadoAttribute() { - return $this->prazo < now(); + return $this->prazo < now(); } - public function getFuturoAttribute() + public function getFuturoAttribute() { - return !$this->passado; + return !$this->passado; } public function scopeFuturos($query) { diff --git a/app/Models/Topico.php b/app/Models/Topico.php index 052ed15..74ce492 100644 --- a/app/Models/Topico.php +++ b/app/Models/Topico.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Stephenjude\DefaultModelSorting\Traits\DefaultOrderBy; class Topico extends Model { @@ -11,6 +12,12 @@ class Topico extends Model protected $table = 'topicos'; protected $guarded = []; + /** + * Set a default ordering for this model + */ + use DefaultOrderBy; + protected static $orderByColumn = 'order'; + // relationships public function exercicios() { diff --git a/app/Models/Turma.php b/app/Models/Turma.php index 6bdcfbb..1dcc15b 100644 --- a/app/Models/Turma.php +++ b/app/Models/Turma.php @@ -32,12 +32,12 @@ public function prazos() public function prazosOrdered() { - // THIS WILL ALWAYS BE ORDERED BY EX-NAME + // THIS WILL ALWAYS BE ORDERED BY PRAZO AND EX-NAME return $this->prazos() ->join('exercicios', 'prazos.exercicio_id', '=', 'exercicios.id') ->select('prazos.*','exercicios.name as exercicio_name') - ->orderBy('exercicio_name') ->orderBy('prazo') + ->orderBy('exercicio_name') ; } } diff --git a/app/Models/User.php b/app/Models/User.php index 3098f10..ee7120c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,11 +7,18 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Hash; +use Stephenjude\DefaultModelSorting\Traits\DefaultOrderBy; class User extends Authenticatable { use HasFactory, Notifiable; + /** + * Set a default ordering for this model + */ + use DefaultOrderBy; + protected static $orderByColumn = 'name'; + /** * Sets a hashed password */ diff --git a/composer.json b/composer.json index e5ad197..fd15e64 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "laravel/framework": "^11.0", "laravel/tinker": "^2.9", "neitanod/forceutf8": "~2.0", + "stephenjude/default-model-sorting": "^3.0", "symfony/yaml": "^6.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 5294604..fff3aba 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d1e9e55d313862befef90771a0dfb213", + "content-hash": "5ae57f9538bb895128ac00e43c60a8fb", "packages": [ { "name": "brick/math", @@ -3209,6 +3209,70 @@ ], "time": "2023-11-08T05:53:05+00:00" }, + { + "name": "stephenjude/default-model-sorting", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/stephenjude/default-model-sorting.git", + "reference": "b60d5d3abeed8267e9b4d3ff0f8ac7fc63e88578" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/stephenjude/default-model-sorting/zipball/b60d5d3abeed8267e9b4d3ff0f8ac7fc63e88578", + "reference": "b60d5d3abeed8267e9b4d3ff0f8ac7fc63e88578", + "shasum": "" + }, + "require": { + "illuminate/support": "^8.0|^9.0|^10.0|^11.0" + }, + "require-dev": { + "orchestra/testbench": "^6.0|^7.0", + "phpunit/phpunit": "^9.0|^10.0|^11.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Stephenjude\\DefaultModelSorting\\DefaultModelSortingServiceProvider" + ], + "aliases": { + "DefaultModelSorting": "Stephenjude\\DefaultModelSorting\\DefaultModelSortingFacade" + } + } + }, + "autoload": { + "psr-4": { + "Stephenjude\\DefaultModelSorting\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Stephen Jude", + "email": "hello@stephenjude.me", + "role": "Developer" + } + ], + "description": "Add default sorting column to your eloquent model", + "homepage": "https://github.com/stephenjude/default-model-sorting", + "keywords": [ + "default-model-sorting", + "eloquent", + "model", + "orderby", + "sorting", + "stephenjude" + ], + "support": { + "issues": "https://github.com/stephenjude/default-model-sorting/issues", + "source": "https://github.com/stephenjude/default-model-sorting/tree/3.0.0" + }, + "time": "2024-03-27T15:06:49+00:00" + }, { "name": "symfony/clock", "version": "v7.0.5",