Skip to content

Commit

Permalink
Formatação da URL gerada na paginação
Browse files Browse the repository at this point in the history
  • Loading branch information
valdineireis committed Oct 20, 2020
1 parent 133d0aa commit 334d2e1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

class TaskController extends Controller
{
public function index()
public function index($page = 1)
{
// $tasks = Task::all();
$tasks = Task::paginate();
$tasks = Task::paginateUri(10, $page);
return view('tasks.index', compact('tasks'));
}

Expand Down
3 changes: 2 additions & 1 deletion app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace App\Models;

use App\Models\Traits\PaginateTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
use HasFactory;
use HasFactory, PaginateTrait;

protected $fillable = ['name', 'status'];
}
24 changes: 24 additions & 0 deletions app/Models/Traits/PaginateTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Models\Traits;

use Illuminate\Pagination\Paginator;

Trait PaginateTrait
{
public static function scopePaginateUri($query, $items, $page)
{
$action = app('request')->route()->getActionName();
$parameters = app('request')->route()->parameters();
$parameters['page'] = '##'; // ## == %23%23
$current_url = action($action, $parameters);
$current_url = preg_replace('/[\?\=]/', '/', $current_url);
Paginator::currentPageResolver(function () use ($page) {
return $page;
});
$paginate = $query->paginate($items);
$links = preg_replace('@href="(.*/?page=(\d+))"@U', 'href="' . str_replace('%23%23', '$2', $current_url) . '"', $paginate->render());
$paginate->linksUri = $links;
return $paginate;
}
}
2 changes: 1 addition & 1 deletion resources/views/tasks/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class="btn btn-sm btn-primary">
</div>

<div class="col-md-12">
{{ $tasks->links() }}
{!! $tasks->linksUri !!}
</div>

</div>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
// DELETE (DELETE)
// Route::delete('tasks/{task}', [TaskController::class, 'destroy'])->name('tasks.destroy');

Route::get('tasks/page/{page}', [TaskController::class, 'index'])->name('tasks.index.page');
Route::resource('tasks', TaskController::class);

Auth::routes();
Expand Down

2 comments on commit 334d2e1

@sta6
Copy link

@sta6 sta6 commented on 334d2e1 Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For append queries to links in pagination

public static function scopePaginateUri($query, $items, $page, $append = [])
{
$action = app('request')->route()->getActionName();
$parameters = app('request')->route()->parameters();
$parameters['page'] = '##'; // ## == %23%23
$current_url = action($action, $parameters);
$current_url = preg_replace('/[?=]/', '/', $current_url);
if ($append) {
foreach ($append as $key => $value) {
if ($key === array_key_first($append))
$current_url .= '?';
else
$current_url .= '&';
$current_url .= $key . '=' . $value;
}
}
Paginator::currentPageResolver(function () use ($page) {
return $page;
});
$paginate = $query->paginate($items);
$links = preg_replace('@href="(.*/?page=(\d+))"@U', 'href="' . str_replace('##', '$2', $current_url) . '"', $paginate->render());
$paginate->linksUri = $links;
return $paginate;
}

@valdineireis
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, @sta6
How can I use this attribute?
Can you show me an exemplo, please?
Thank you for your contribution.

Please sign in to comment.