i want to show my products data when the user_id (foreign key) in products table same with id (primary key) on users table, i use laravel livewire datatable
how to use this function
public function query(): Builder
{
$user_id = auth()->user()->id;
return Product::query()->where('user_id', $user_id);
}
in this function
public function columns(): array
{
return [
Column::make("Nama", "name")
->sortable()
->searchable(),
Column::make("Deskripsi", "description")
->sortable()
->searchable(),
Column::make("Stok", "stock")
->sortable()
->format(
fn($value, $row, Column $column) => $row->stock === 9999 ? '<span class="badge bg-label-success">INSTOCK</span>' : number_format($row->stock, 0, ',', '.')
)->html(),
];
}
the code you used is wrong as described in this documentation
this is the correct code:
public function builder(): Builder
{
$user_id = auth()->user()->id;
return Product::query()
->where('user_id', $user_id);
}