#Incorrect types stated

2 messages · Page 1 of 1 (latest)

noble kindle
#

I'm having a problem with casting the correct types for return values I'm getting the following errors from the WithBulkActions trait. I've added the trait in a comment to this post.

Call to an undefined method Illuminate\Contracts\Pagination\LengthAwarePaginator::pluck().  
Call to an undefined method Illuminate\Database\Query\Builder::whereKey().
<?php

declare(strict_types=1);

namespace App\Http\Livewire\Events;

use App\Http\Livewire\BaseComponent;
use App\Http\Livewire\Datatable\WithBulkActions;
use App\Http\Livewire\Datatable\WithSorting;
use App\Models\Event;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Livewire\Attributes\Computed;

/**
 * @property LengthAwarePaginator $rows
 * @property Builder $rowsQuery
 */
class EventsList extends BaseComponent
{
    use WithBulkActions;
    use WithSorting;

    #[Computed]
    public function rowsQuery(): EloquentBuilder
    {
        $query = Event::query()
            ->when($this->filters['search'], fn (EloquentBuilder $query, string $search) => $query->where('name', 'like', '%'.$search.'%'))
            ->oldest('name');

        return $this->applySorting($query);
    }

    /**
     * Undocumented function.
     */
    #[Computed]
    public function rows(): LengthAwarePaginator
    {
        return $this->applyPagination($this->rowsQuery);
    }

    ...
}
#
<?php

declare(strict_types=1);

namespace App\Http\Livewire\Datatable;

use Illuminate\Database\Query\Builder;
use Livewire\Attributes\Computed;
use Illuminate\Support\Collection;

/**
 * @property Builder $selectedRowsQuery
 */
trait WithBulkActions
{
    /**
     * Undocumented variable.
     */
    public bool $selectPage = false;

    /**
     * Undocumented variable.
     */
    public bool $selectAll = false;

    /**
     * Undocumented variable.
     *
     * @var \Illuminate\Support\Collection<int, string>
     */
    public Collection $selected;

    /**
     * Undocumented function.
     */
    public function bootWithSorting(): void
    {
        $this->selected = collect();
    }

    /**
     * Undocumented function.
     */
    public function renderingWithBulkActions(): void
    {
        if ($this->selectAll) {
            $this->selectPageRows();
        }
    }

    /**
     * Undocumented function.
     */
    public function updatedSelected(): void
    {
        $this->selectAll = false;
        $this->selectPage = false;
    }

    /**
     * Undocumented function.
     */
    public function updatedSelectPage(int $value): void
    {
        if ($value) {
            $this->selectPageRows();

            return;
        }

        $this->selectAll = false;
        $this->selected = collect();
    }

    /**
     * Undocumented function.
     */
    public function selectPageRows(): void
    {
        $this->selected = $this->rows->pluck('id')->map(fn ($id) => (string) $id);
    }

    /**
     * Undocumented function.
     */
    public function selectAll(): void
    {
        $this->selectAll = true;
    }

    /**
     * Undocumented function.
     */
    #[Computed]
    public function selectedRowsQuery(): Builder
    {
        return (clone $this->rowsQuery)
            ->unless($this->selectAll, fn ($query) => $query->whereKey($this->selected));
    }
}