Home > Net >  Laravel Livewire: Applying the pagination as object
Laravel Livewire: Applying the pagination as object

Time:08-18

In the application, there's a button "Add" where it will popup a modal, there's client dropdown and date.

enter image description here

I put some wire:change events for my client dropdown and date,once it is filled up, it will show all the related records in the table. I decided to use a pagination for it but having a problem implementing the pagination.

Livewire

public function render()
{
    return view('livewire.billings.delivery-receipts-lists');
}

public function mount()
{
    
    $this->clients                                                  = Client::where('company_id', Auth::user()->company_id)->where('active',0)->get();
    $this->start_date                                               = date('Y-m-d');
    $this->end_date                                                 = date('Y-m-d', strtotime(" 7 days", strtotime($this->start_date)));

    $this->filter = [
        "Quotation Number"                                          => true,
        "Quotation Date"                                            => true,
        'Customer'                                                  => true,
        'Project'                                                   => true,
        'Date'                                                      => true,
        'Time'                                                      => true,
        'Prepared by'                                               => true,
        'Status'                                                    => true,
        'Level'                                                     => true,
        'Actions'                                                   => true,
    ];
}

public function changeEndDate()
{
    $this->end_date                                                 = date('Y-m-d', strtotime(" 7 days", strtotime($this->start_date)));
    $this->checkClientAndDate();
}

public function checkClientAndDate()
{
    $this->generate                                                 = false;
    if($this->client_id && $this->start_date)
    {
        $this->generate                                             = true;

    }

    $this->getAllDeliveryReceipts();
}
public function getAllDeliveryReceipts()
{
    if($this->generate == true)
    {
        $this->delivery_receipts                                    = DeliveryReceipts::where('client_id',$this->client_id)
                                                                        ->where('level_id',4)
                                                                        ->whereBetween('dr_date', [$this->start_date, $this->end_date])
                                                                        ->where('company_id', Auth::user()->company_id)
                                                                        ->search(trim($this->search))
                                                                        ->orderBy($this->column, $this->order)
                                                                        ->paginate($this->size);
    }
}

Question: How can I apply the pagination in my delivery_receipts that is not in the render method?

CodePudding user response:

Simple answer: You don't. Livewire won't work with pagination somewhere else. That's the difficult part of Livewire being stateless.

Complex answer: Build your own paginator by setting a page variable and incrementing/decrementing it as needed, and use it to build your own limit offset query.

  • Related