Complex

id
ID
Planet
Region
Name
Planet Mates
Car
Post Count
Weapon Names
Weapon Count
Computed (php closure)
Computed (raw SQL)
Email Verified
DOB
Birthday
Age
Orbital_period
native_age
Bedtime
Go to bed
Email
Bio
Role
Weapons
Light Saber
0
SELECT ALL
Aleen Minor
Colonies
, aDr. Tyson Wiz...
0
0
User 1985 hails from Aleen Minor
User 1985 hails from Aleen Minor
15/10/1995
15th October
25
02:10
Iure odit dolorum so...
Jumptrooper
Alderaan
Inner Rim
Amelie Lehner, A...
0
0
User 1984 hails from Alderaan
User 1984 hails from Alderaan
20/04/1972
20th April
49
364
02:12
Harum et mollitia oc...
AT-AT Pilot
Dagobah
Deep Core
Bernard Satterfi...
0
0
User 1983 hails from Dagobah
User 1983 hails from Dagobah
18/01/1942
18th January
79
341
19:53
Accusantium omnis ea...
AT-AT Pilot
Alderaan
Inner Rim
Amelie Lehner, A...
0
0
User 1982 hails from Alderaan
User 1982 hails from Alderaan
25/08/1970
25th August
50
364
23:02
Molestiae distinctio...
Imperial Guard
Hoth
Expansion Region
Adan Gerlach, Am...
0
0
User 1981 hails from Hoth
User 1981 hails from Hoth
08/08/1962
8th August
58
549
22:47
Optio aut et ipsam c...
Imperial Guard
Cato Neimoidia
Inner Rim
Alek Schmeler ds...
0
0
User 1980 hails from Cato Neimoidia
User 1980 hails from Cato Neimoidia
02/04/1979
2nd April
42
278
22:06
Rerum numquam ipsa q...
Shock Trooper
Bespin
Inner Rim
asd, Candido Koh...
0
0
User 1979 hails from Bespin
User 1979 hails from Bespin
07/05/1959
7th May
62
5110
21:55
Qui deleniti quisqua...
Shili
Mid Rim
Althea Deckow I,...
0
0
User 1976 hails from Shili
User 1976 hails from Shili
02/04/1984
2nd April
37
23:41
Quisquam ut sint ass...
Naboo
Wild Space
gerg er gDrewas...
0
0
User 1975 hails from Naboo
User 1975 hails from Naboo
08/12/1927
8th December
93
312
01:46
Sit quibusdam quos i...
Shock Trooper
Shili
Mid Rim
Althea Deckow I,...
0
0
User 1974 hails from Shili
User 1974 hails from Shili
22/11/1928
22nd November
92
19:23
Aspernatur maiores r...
Shock Trooper
Results 11 - 20 of 1993
Array
(
)
<livewire:complex-demo-table />
<?php

namespace App\Http\Livewire;

use App\User;
use App\Planet;
use App\Region;
use App\Weapon;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Mediconesystems\LivewireDatatables\Column;
use Mediconesystems\LivewireDatatables\DateColumn;
use Mediconesystems\LivewireDatatables\TimeColumn;
use Mediconesystems\LivewireDatatables\NumberColumn;
use Mediconesystems\LivewireDatatables\BooleanColumn;
use Mediconesystems\LivewireDatatables\Http\Livewire\LivewireDatatable;

class ComplexDemoTable extends LivewireDatatable
{
    public $hideable = 'select';
    public $exportable = true;
    public $afterTableSlot = 'components.selected';

    public function builder()
    {
        return User::query()->leftJoin('planets', 'planets.id', 'users.planet_id');
    }

    public function columns()
    {
        return [
            Column::checkbox(),

            NumberColumn::name('id')
                ->label('ID')
                ->filterable()
                ->linkTo('user', 6),

            Column::name('planets.name')
                ->label('Planet')
                ->filterable($this->planets)->alignRight(),

            Column::name('planet.region.name')
                ->label('Region')
                ->filterable($this->regions)
                ->searchable(),

            Column::name('name')
                ->filterable()
                ->editable(),

            Column::name('comrades.name')
                ->label('Planet Mates')
                ->truncate()
                ->filterable(),

            Column::name('car.model')
                ->label('Car')
                ->alignCenter()
                ->filterable(['Audi', 'BMW', 'Caterham', 'Dodge', 'Ferrari', 'Jaguar', 'Lamborghini', 'Porsche']),

            NumberColumn::name('posts.id:count')
                ->label('Post Count')
                ->filterable()
                ->alignRight(),

            Column::name('weapons.name')
                ->filterable($this->weapons->pluck('name'))
                ->label('Weapon Names'),

            NumberColumn::name('weapons.id')
                ->filterable()
                ->label('Weapon Count'),

            Column::callback(['id', 'planet.name'], function ($id, $planetName) {
                return "User $id hails from $planetName";
            })->label('Computed (php closure)')
                ->filterable(),

            Column::raw('CONCAT("User ", users.id, " hails from ", planets.name) AS planetName')
                ->label('Computed (raw SQL)')
                ->filterable(),

            BooleanColumn::name('email_verified_at')
                ->label('Email Verified')
                ->filterable(),

            DateColumn::name('dob')
                ->label('DOB')
                ->filterable(),

            DateColumn::raw('dob AS dob2')
                ->label('Birthday')
                ->format('jS F')
                ->sortBy(DB::raw('DATE_FORMAT(users.dob, "%m%d%Y")')),

            NumberColumn::raw('FLOOR(DATEDIFF(NOW(), users.dob)/365) AS Age')
                ->filterable(),

            NumberColumn::name('planet.orbital_period')
                ->filterable(),

            Column::raw("IF(planets.orbital_period REGEXP '^-?[0-9]+$', CONCAT(ROUND(DATEDIFF(NOW(), users.dob) / planets.orbital_period, 1), ' ', planets.name, ' years'), '---') AS native_age")
                ->filterable()
                ->hide(),

            TimeColumn::name('bedtime')
                ->filterable(),

            Column::callback('bedtime', 'computeBedtime')
                ->label('Go to bed')
                ->hide(),

            Column::name('email')
                ->searchable()
                ->filterable()
                ->view('components.email'),

            Column::name('bio')
                ->truncate(20)
                ->filterable(),

            Column::name('role')
                ->searchable()
                ->filterable([
                    'Stormtrooper',
                    'AT-AT Pilot',
                    'AT-ST Driver',
                    'Imperial Guard',
                    'Shock Trooper',
                    'Shadow Trooper',
                    'Purge Trooper',
                    'Jumptrooper'
                ]),

            Column::scope('selectGroupedWeaponNames', 'Weapons')
                ->filterable($this->weapons, 'filterWeaponNames'),

            BooleanColumn::scope('hasLightSaber', 'Light Saber')
                ->filterable(null, 'filterHasLightSaber')
        ];
    }

    public function getPlanetsProperty()
    {
        return Planet::pluck('name');
    }

    public function getRegionsProperty()
    {
        return Region::pluck('name');
    }

    public function getWeaponsProperty()
    {
        return Weapon::all();
    }

    public function computeBedtime($date)
    {
        if (!$date) {
            return;
        }
        return Carbon::parse($date)->isPast()
            ? Carbon::parse($date)->addDay()->diffForHumans(['parts' => 2])
            : Carbon::parse($date)->diffForHumans(['parts' => 2]);
    }
}
<?php

namespace App;

use App\Planet;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, SoftDeletes;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function car()
    {
        return $this->hasOne(Car::class);
    }

    public function comrades()
    {
        return $this->hasManyThrough(User::class, Planet::class, 'id', 'planet_id', 'planet_id', 'id');
    }

    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function weapons()
    {
        return $this->belongsToMany(Weapon::class);
    }

    public function planet()
    {
        return $this->belongsTo(Planet::class);
    }

    public function region()
    {
        return $this->hasOneThrough(Region::class, Planet::class, 'id', 'id', 'planet_id', 'region_id');
    }

    public function scopeSelectGroupedWeaponNames($query, $alias)
    {
        $query->addSelect([
            $alias => Weapon::selectRaw('GROUP_CONCAT(name SEPARATOR " | ")')
                ->leftJoin('user_weapon', 'user_weapon.weapon_id', 'weapons.id')
                ->whereColumn('user_weapon.user_id', 'users.id')
        ]);
    }

    public function scopeFilterWeaponNames($query, $value)
    {
        $query->whereHas('weapons', function ($query) use ($value) {
            $query->where('weapons.id', $value);
        });
    }

    public function scopeHasLightSaber($query, $alias)
    {
        $query->addSelect([
            $alias => Weapon::selectRaw('IF(COUNT(weapons.id), 1, 0)')
                ->leftJoin('user_weapon', 'user_weapon.weapon_id', 'weapons.id')
                ->whereColumn('user_weapon.user_id', 'users.id')
                ->where('weapons.name', 'Light Saber')
        ]);
    }

    public function scopeFilterHasLightSaber($query, $value)
    {
        $query->whereHas('weapons', function ($query) use ($value) {
            $query->where('weapons.name', $value ? '=' : '<>', 'Light Saber');
        });
    }
}