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
Dorin
Wild Space
Albin Waelchi, A...
0
0
User 1999 hails from Dorin
User 1999 hails from Dorin
24/05/1947
24th May
74
409
03:08
Velit odit et dolor...
Shock Trooper
Kalee
Expansion Region
Aiden Ondricka D...
0
0
User 1998 hails from Kalee
User 1998 hails from Kalee
07/03/1988
7th March
33
378
02:38
Corrupti non quas su...
Purge Trooper
Rodia
Expansion Region
/+okok sdgsdg, A...
0
0
User 1997 hails from Rodia
User 1997 hails from Rodia
04/11/2012
4th November
8
305
19:51
Suscipit totam odit...
Jumptrooper
Naboo
Wild Space
gerg er gDrewas...
0
0
User 1996 hails from Naboo
User 1996 hails from Naboo
10/12/1985
10th December
35
312
19:19
Velit aspernatur vol...
AT-ST Driver
Eriadu
Expansion Region
, Beryl Hickle,...
0
0
User 1995 hails from Eriadu
User 1995 hails from Eriadu
28/06/1925
28th June
96
360
23:30
Voluptas eius necess...
Purge Trooper
Shili
Mid Rim
Althea Deckow I,...
0
0
User 1994 hails from Shili
User 1994 hails from Shili
08/02/1966
8th February
55
23:56
Voluptates dolores o...
Stormtrooper
Alderaan
Inner Rim
Amelie Lehner, A...
0
0
User 1993 hails from Alderaan
User 1993 hails from Alderaan
16/10/2006
16th October
14
364
01:13
Quae placeat dolorem...
Bespin
Inner Rim
asd, Candido Koh...
0
0
User 1992 hails from Bespin
User 1992 hails from Bespin
08/09/1938
8th September
82
5110
21:49
Soluta sequi unde la...
Imperial Guard
Kashyyyk
Unknown Regions
Aliyah Lockman,...
0
0
User 1991 hails from Kashyyyk
User 1991 hails from Kashyyyk
06/04/2014
6th April
7
381
02:20
Occaecati harum qui...
Jumptrooper
Aleen Minor
Colonies
, aDr. Tyson Wiz...
0
0
User 1990 hails from Aleen Minor
User 1990 hails from Aleen Minor
05/10/1947
5th October
73
20:33
Magnam ex consequunt...
Shock Trooper
Previous
Results 1 - 10 of 1999
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');
        });
    }
}