Skip to content

Commit b471f02

Browse files
committed
DateColumn Fixes, DateTimeColumn Addition
1 parent 12b39cc commit b471f02

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Views/Columns/DateColumn.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\Carbon;
66
use DateTime;
7+
use Carbon\Exceptions\InvalidFormatException;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Support\HtmlString;
910
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
@@ -36,10 +37,36 @@ public function getContents(Model $row): null|string|\BackedEnum|HtmlString|Data
3637
return $dateTime->format($this->getOutputFormat());
3738
} else {
3839
// Check if format matches what is expected and return Carbon instance if so, otherwise emptyValue
39-
return Carbon::canBeCreatedFromFormat($dateTime, $this->getInputFormat()) ? Carbon::createFromFormat($this->getInputFormat(), $dateTime)->format($this->getOutputFormat()) : $this->getEmptyValue();
40+
if (Carbon::canBeCreatedFromFormat($dateTime, $this->getInputFormat()))
41+
{
42+
return Carbon::createFromFormat($this->getInputFormat(), $dateTime)->format($this->getOutputFormat());
43+
}
44+
else
45+
{
46+
try {
47+
if ($date = Carbon::parse($dateTime))
48+
{
49+
return $date->format($this->getOutputFormat());
50+
}
51+
}
52+
catch (InvalidFormatException $e)
53+
{
54+
return $this->getEmptyValue();
55+
}
56+
catch (\Exception $e)
57+
{
58+
return $this->getEmptyValue();
59+
}
60+
}
61+
return $this->getEmptyValue();
4062
}
4163
}
42-
} catch (\Exception $exception) {
64+
}
65+
catch (InvalidFormatException $e)
66+
{
67+
return $this->getEmptyValue();
68+
}
69+
catch (\Exception $exception) {
4370
return $this->getEmptyValue();
4471
}
4572

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Columns;
4+
5+
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Configuration\DateColumnConfiguration;
6+
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\{HasInputOutputFormat, IsColumn};
7+
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers\DateColumnHelpers;
8+
9+
class DateTimeColumn extends DateColumn
10+
{
11+
use IsColumn,
12+
HasInputOutputFormat,
13+
DateColumnConfiguration,
14+
DateColumnHelpers { DateColumnHelpers::getValue insteadof IsColumn; }
15+
16+
public string $inputFormat = 'Y-m-d H:i:s';
17+
18+
public string $outputFormat = 'Y-m-d H:i:s';
19+
20+
public string $emptyValue = '';
21+
22+
protected string $view = 'livewire-tables::includes.columns.date';
23+
24+
}

0 commit comments

Comments
 (0)