I used Laravel v8x, with Livewire and I need to use pagination with this livewire. When I try to make a pagination page from the admins' collection, I got this error. I need to make pagination with livewire search
Blade
<div >
<div id="DataTables_Table_0_wrapper" >
<livewire:admin-search />
</div>
</div>
Livewire blade
<div>
{{-- Stop trying to control. --}}
<div >
<div >
<div id="DataTables_Table_0_length"><label>عرض <select wire:model="row_no"
name="DataTables_Table_0_length" aria-controls="DataTables_Table_0" >
@if ($admin_count <= 15)
<option value="15">15</option>
@else
@for ($i = 1; $i <= $admin_count; $i)
@if ($i % 15 == 0)
<option value="{{ $i }}">{{ $i }}</option>
@elseif ($i 1 > $admin_count)
<option value="{{ $i }}">{{ $i }}</option>
@endif
@endfor
@endif
</select> المسؤولين</label></div>
</div>
<div >
<div
>
<div >
<div id="DataTables_Table_0_filter" >
<label>بحث :<input type="search" placeholder="" wire:model="searchTerm"
aria-controls="DataTables_Table_0"></label>
</div>
</div>
@can('Create-admin')
<div ><button tabindex="0"
aria-controls="DataTables_Table_0" type="button" data-bs-toggle="modal"
data-bs-target="#modals-slide-in"><span>اضافة مسؤول جديد</span></button>
</div>
@endcan
</div>
</div>
</div>
<table id="DataTables_Table_0" role="grid"
aria-describedby="DataTables_Table_0_info">
<thead >
<tr role="row">
<th rowspan="1" colspan="1"
style="width: 47.3281px; display: none;" aria-label=""></th>
<th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1"
style="width: 5px;" aria-label="User: activate to sort column ascending">#</th>
<th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1"
style="width: 109.984px;" aria-label="User: activate to sort column ascending">الاسم</th>
<th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1"
colspan="1" style="width: 123.109px;" aria-sort="descending"
aria-label="Email: activate to sort column ascending">البريد</th>
<th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1"
colspan="1" style="width: 123.109px;" aria-sort="descending"
aria-label="Email: activate to sort column ascending">الهاتف</th>
<th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1"
colspan="1" style="width: 123.109px;" aria-sort="descending"
aria-label="Email: activate to sort column ascending">العمر</th>
<th tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1"
style="width: 139.875px;" aria-label="Status: activate to sort column ascending">حالة الحساب</th>
@canany(['Update-admin', 'Delete-admin'])
<th rowspan="1" colspan="1" style="width: 173.734px;"
aria-label="Actions">الإعدادات</th>
@endcanany
</tr>
</thead>
<tbody>
@php
$counter = 1;
@endphp
@foreach ($admins as $admin)
<tr>
<td>
{{ $admin->id }}
</td>
<td>
{{ $admin->fname . ' ' . $admin->lname }}
</td>
<td>
{{ $admin->email }}
</td>
<td>
{{ $admin->phone }}
</td>
<td>
{{ $admin->age }}
</td>
<td>
<span
>{{ $admin->status }}</span>
</td>
@canany(['Update-admin', 'Delete-admin'])
<td>
<div role="group" aria-label="Basic example">
@can('Update-admin')
<a href="{{ route('admins.edit', Crypt::encrypt($admin->id)) }}"
>تعديل</a>
@endcan
@can('Delete-admin')
<button type="button" onclick="confirmDestroy({{ $admin->id }}, this)"
>حذف</button>
@endcan
</div>
</td>
@endcanany
</tr>
@php
$counter;
@endphp
@endforeach
<tr wire:loading>
<td valign="top" colspan="6" >يبحث...</td>
</tr>
</tbody>
</table>
</div>
<div >
<div >
<div >
{{ $admins->links() }}
</div>
</div>
</div>
Livewire Compnent
<?php
namespace App\Http\Livewire;
use App\Models\Admin;
use Livewire\Component;
use Livewire\WithPagination;
class AdminSearch extends Component
{
use WithPagination;
public $searchTerm;
public $row_no = 10;
public $admins;
public $admin_count = 0;
public function mount()
{
$this->admin_count = Admin::getByAuthUserInfo()->count();
}
public function render()
{
$this->admins = Admin::orWhere([
['fname', 'Like', '%' . $this->searchTerm . '%'],
])
->where('user_id', auth('user')->user()->id)
->orderBy('fname')
->take($this->row_no)
->paginate(12);
return view('livewire.admin-search');
}
}
And the index function from the AdminController
public function index()
{
// Check Ability
$this->checkUserAbility('Read-admin');
//
$admins = Admin::getByAuthUserInfo()->get();
$roles = Role::where('guard_name', 'admin')->get();
// Store Log
$this->storeUserLogs('browse admins');
return response()->view('cms.admins.index', [
'admins' => $admins,
'password' => $this->generateNewPassword(8),
'roles' => $roles,
]);
}
And also I tried to make it from the index function in the controller, and it work correctly but it doesn't change the table content in the table
Index controller function in AdminController
public function index()
{
// Check Ability
$this->checkUserAbility('Read-admin');
//
$admins = Admin::getByAuthUserInfo()->paginate(12);
$roles = Role::where('guard_name', 'admin')->get();
// Store Log
$this->storeUserLogs('browse admins');
return response()->view('cms.admins.index', [
'admins' => $admins,
'password' => $this->generateNewPassword(8),
'roles' => $roles,
]);
}
Result images
CodePudding user response:
hey you solution is very simple, you got this error because livewire cannot defined collection var after page render
instead of define admins variable in public of component just pass by view in render function
$admins = Admin::orWhere([
['fname', 'Like', '%' . $this->searchTerm . '%'],
])
->where('user_id', auth('user')->user()->id)
->orderBy('fname')
->take($this->row_no)
->paginate(12);
return view('livewire.admin-search',['admins']);
and delete your public variable