Search code examples
phplaraveldatatablelaravel-8laravel-livewire

Rappasoft Datatables V2 (I want to show data when another table has same value column)


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(),
        ];
    }

Solution

  • 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);
    }