Adding a table to a Livewire component

Typed property App\Livewire\Company\CompanyList::$user must not be accessed before initialization I got the above error. I'm trying to show the users who belong to companies. I've no idea.
<?php

namespace App\Livewire\Company;


class CompanyList extends Component implements Forms\Contracts\HasForms, Tables\Contracts\HasTable
{
use Forms\Concerns\InteractsWithForms;
use Tables\Concerns\InteractsWithTable;

public User $user;

public function table(Table $table): Table
{
return $table
->relationship(fn (): HasMany => $this->user->companies())
->columns([
Tables\Columns\ImageColumn::make('company_logo_path')
->label('Company Logo'),
Tables\Columns\TextColumn::make('name')
->label(__('translations.field.company_name')),
Tables\Columns\TextColumn::make('email')
->label(__('translations.field.email')),
Tables\Columns\TextColumn::make('location')
->label(__('translations.field.location')),
]),
//
}

public function render(): View
{
return view('livewire.company.company-list')->layout('layouts.profile-layout');
}
}
<?php

namespace App\Livewire\Company;


class CompanyList extends Component implements Forms\Contracts\HasForms, Tables\Contracts\HasTable
{
use Forms\Concerns\InteractsWithForms;
use Tables\Concerns\InteractsWithTable;

public User $user;

public function table(Table $table): Table
{
return $table
->relationship(fn (): HasMany => $this->user->companies())
->columns([
Tables\Columns\ImageColumn::make('company_logo_path')
->label('Company Logo'),
Tables\Columns\TextColumn::make('name')
->label(__('translations.field.company_name')),
Tables\Columns\TextColumn::make('email')
->label(__('translations.field.email')),
Tables\Columns\TextColumn::make('location')
->label(__('translations.field.location')),
]),
//
}

public function render(): View
{
return view('livewire.company.company-list')->layout('layouts.profile-layout');
}
}
2 Replies
krekas
krekas15mo ago
$user must have a value. in your case I guess to need to set it up in the mount()
Shaung Bhone
Shaung BhoneOP15mo ago
return $table
->query(auth()->user()->companies()->getQuery())
return $table
->query(auth()->user()->companies()->getQuery())
Want results from more Discord servers?
Add your server