Anyway, it looks like I need to use a ResourceCollection
Why do you believe that; you are working with Blade, not with an API?
I have a companies component and been trying to get this to work for what seems like at least a week.
[rant]The Laravel docs are good for some things however it's missing a lot including more FULL examples, not vague partial examples. I've run into this many times and I'm left to seek out other sources for information. [/rant]
Anyway, it looks like I need to use a ResourceCollection . The only thing is that it's not working either so here's what my code looks like. Could someone help me fix this please?
Companies Livewire Component : Companies.php
namespace App\Http\Livewire\Admin;
use App\Http\Resources\CompanyResource;
use App\Models\Company;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;
use Livewire\WithPagination;
use Log;
class Companies extends Component
{
use WithPagination;
/**
* Company List Properties
*/
public int $currentPage = 1;
public string $success = '';
public int $perPage = 10;
public bool $showModal = false;
public bool $createOrdEditModal = false;
public $companies = null;
public ?Company $company = null;
public int $companyId = 0;
public string $name = '';
public string $street_address = '';
public string $bldg_ste_rm = '';
public string $city = '';
public string $state = '';
public string $postal_code = '';
public string $contact_name = '';
public string $contact_phone = '';
public string $contact_email = '';
public string $webmaster_phone = '';
public string $webmaster_email = '';
public string $landing_page_url = '';
public array $pages = [
1 => [
'heading' => 'Company Information',
'subheading' => 'Enter Company Information To Get Started.',
],
2 => [
'heading' => 'Company Contact Information',
'subheading' => 'Create Company Contact Information',
],
];
private array $validationRules = [
1 => [
'company_name' => ['required'],
'street_address' => ['required'],
'bldg_ste_rm' => ['required'],
'city' => ['required'],
'state' => ['required'],
'postal_code' => ['required'],
'landing_page_url' => ['required'],
],
2 => [
'contact_name' => ['required'],
'contact_phone' => ['required'],
'contact_email' => ['required'],
'webmaster_phone' => ['required'],
'webmaster_email' => ['required'],
],
];
public function render()
{
$companies = CompanyResource::collection(Company::paginate($this->perPage));
$this->companies = response()->json($companies->resource);
return view('livewire.admin.companies');
}
public function updated(): void
{
}
public function refreshList(): void
{
}
public function create(): void
{
$this->resetInputFields();
$this->createOrdEditModal = true;
$this->openModal();
}
public function openModal(): void
{
$this->showModal = true;
$this->resetErrorBag();
$this->resetValidation();
}
public function closeModal(): void
{
$this->showModal = false;
$this->createOrdEditModal = false;
}
private function resetInputFields(): void
{
$this->companyId = 0;
$this->name = '';
$this->street_address = '';
$this->bldg_ste_rm = '';
$this->city = '';
$this->state = '';
$this->postal_code = '';
$this->contact_name = '';
$this->contact_phone = '';
$this->contact_email = '';
$this->webmaster_phone = '';
$this->webmaster_email = '';
$this->landing_page_url = '';
}
public function goToNextPage(): void
{
$this->validate($this->validationRules[$this->currentPage]);
$this->currentPage++;
}
public function goToPreviousPage(): void
{
$this->currentPage--;
}
public function resetSuccess(): void
{
$this->reset('success');
}
public function submit(): void
{
$rules = collect($this->validationRules)->collapse()->toArray();
$this->validate($rules);
$company = Company::updateOrCreate([
'name' => $this->name,
'street_address' => $this->street_address,
'bldg_ste_rm' => $this->bldg_ste_rm,
'city' => $this->city,
'state' => $this->state,
'postal_code' => $this->postal_code,
'landing_page_url' => $this->landing_page_url,
'contact_name' => $this->contact_name,
'contact_phone' => $this->contact_phone,
'contact_email' => $this->contact_email,
'webmaster_phone' => $this->webmaster_phone,
'webmaster_email' => $this->webmaster_email,
]);
$this->reset();
$this->resetValidation();
$this->success = 'Company created successfully!';
}
public function edit(Company $company): void
{
$this->company = $company;
$this->createOrdEditModal = true;
$this->openModal();
}
public function view(Company $company): void
{
$this->company = $company;
$this->openModal();
}
public function delete(Company $company): void
{
//$this->company_id = $id;
//$company = Company::find($id);
$company->delete();
// $this->refreshList();
session()->flash('message', 'Company deleted successfully.');
}
}
App/Http/Resources/CompanyResource.php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CompanyResource extends ResourceCollection
{
public function toArray($request): array
{
// return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
'street_address' => $this->streetAddress,
'bldg_ste_rm' => $this->bldg_ste_rm,
'city' => $this->city,
'state' => $this->state,
'postal_code' => $this->postcode,
'landing_page_url' => $this->url,
'contact_name' => $this->contact_name,
'contact_phone' => $this->contact_phone,
'contact_email' => $this->contact_email,
'webmaster_phone' => $this->webmaster_phone,
'webmaster_email' => $this->webmaster_email
];
}
}
Finally! I'm posting this here for anyone else that runs into this massive headache. I followed the solution on this webpage: https://www.infyom.com/blog/how-to-build-pagination-with-laravel-livewire
The way this developer describes the solution is this:
use Livewire\WithPagination;
use Livewire\Component;
use App\User;
class UserListing extends Component
{
use WithPagination;
public function render()
{
return view('livewire.users.index', [
'users' => User::paginate(10);
]);
}
}
In the blade template:
@forelse ($users as $user)
{{ $user->name }}
@empty
{{ __('No Data Found') }}
@endforelse
{{ $users->links() }}
Granted, this is simple, but that's all I'm looking for at the moment.
Hopefully this will help someone.
Please or to participate in this conversation.