summaryrefslogtreecommitdiff
path: root/workbench/app/Livewire/OrdersTable.php
blob: 431e8a807a3ebc7a81ed8c663089c90aa0f45ce1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

namespace Workbench\App\Livewire;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use Lightscale\LaralightTables\Columns\Column;
use Lightscale\LaralightTables\Toolbar;
use Lightscale\LaralightTables\Toolbar\Dropdown;
use Lightscale\LaralightTables\Toolbar\Dropdown\Button;
use Lightscale\LaralightTables\Toolbar\Dropdown\Link;
use Workbench\App\Models\Order;

/**
 * @extends Table<Order>
 */
class OrdersTable extends Table
{
    protected $model = Order::class;

    /**
     * @return Builder<Order>
     */
    protected function query(): Builder
    {
        return Order::withCount('products');
    }

    public function toolbars(): array
    {
        return [
            Toolbar::make($this)
                ->appendEnd(Dropdown::make()
                    ->attributes(['class' => 'btn-primary'])
                    ->slot('Dropdown')
                    ->items([
                        Button::make()
                            ->slot('Item button 1')
                            ->action(['dropdownAction', 'World']),
                        Link::make()
                            ->slot('Products')
                            ->href(route('products')),
                    ])
                ),
        ];
    }

    public function dropdownAction(string $val): void
    {
        $this->js("alert('hello {$val}')");
    }

    public function columns(): array
    {
        return [
            Column::make('id', 'ID'),
            Column::make('status', 'Status')
                ->slot(fn (Order $r) => ucfirst($r->status->value)),
            Column::make('products_count', 'Product Count'),
            Column::make('total', 'Total Price')
                ->slot(fn (Order $r) => new HtmlString(
                    '<span style="color:red">£'.number_format((float) $r->total, 2).'</span>'
                )),
        ];
    }
}