R
R
Roman2019-03-02 14:36:02
Laravel
Roman, 2019-03-02 14:36:02

Is it possible to sort in reverse order on paginate() without using the DB:: facade?

Hello.
I need to display records from the database in reverse order and with pagination.
The only method I've found so far is something like:

$tickets = DB::table('ticket')->orderBy('id', 'desc')->paginate(10);

But I see it too somehow .. a crutch, or something)) I want to do it using a model, not a query constructor.
Are there such options?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav M., 2019-03-02
@procode

You can use the Eloquent model rather than the query builder. (through facade)
You can also inject a dependency through the constructor:

class TicketController extends Controller
{
    protected $ticket;
    
    public function __construct(Ticket $ticket)
    {
        $this->ticket = $ticket;
    }

    // для примера метод index
    public function index()
    {
        $tickets = $this->ticket->orderBy('id', 'desc')->paginate(10);

        return view('tickets.index', [
                'tickets' => $tickets,
        ]);
    }
}

In general, the principle is the same everywhere. This is without going into details.
Do not forget to specify the namespaces in the files where the files are located!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question