Skip to content

Commit 1a15be7

Browse files
authored
Add fix for DateColumn when empty (rappasoft#1726)
* Add fix for DateColumn when empty --------- Co-authored-by: lrljoe <[email protected]>
1 parent 6e4ce7d commit 1a15be7

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
66
## [v3.2.6] - UNRELEASED
77
### New Features
88
- Add configurable wire:model for filters by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1699
9+
- Customisable Model paths for Make command by @marvoh in https://github.com/rappasoft/laravel-livewire-tables/pull/1714
10+
11+
### Bug Fixes
12+
- Fix error with DateColumn when empty by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1726
13+
14+
### Tweaks
15+
- Migrate to PHPUnit Attributes rather than Doc Comments by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1727
16+
- Remove broken test by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1719
17+
918

1019
## [v3.2.5] - 2024-04-30
1120
### New Features

src/Commands/MakeCommand.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ public function getModelImport(): string
130130
$filename = rtrim($this->modelPath, '/').'/'.$this->model.'.php';
131131
if (File::exists($filename)) {
132132
//In case the file has more than one class which is highly unlikely but still possible
133-
$classes = array_filter($this->getClassesList($filename), function($class) {
134-
return substr($class,strrpos($class,'\\')+1) == $this->model;
133+
$classes = array_filter($this->getClassesList($filename), function ($class) {
134+
return substr($class, strrpos($class, '\\') + 1) == $this->model;
135135
});
136-
if(count($classes) == 1)
136+
if (count($classes) == 1) {
137137
return $classes[0];
138+
}
138139
}
139140
}
140141

@@ -148,9 +149,9 @@ public function getModelImport(): string
148149
*/
149150
private function getClassesList($file): array
150151
{
151-
$classes = [];
152+
$classes = [];
152153
$namespace = '';
153-
$tokens = \PhpToken::tokenize(file_get_contents($file));
154+
$tokens = \PhpToken::tokenize(file_get_contents($file));
154155

155156
for ($i = 0; $i < count($tokens); $i++) {
156157
if ($tokens[$i]->getTokenName() === 'T_NAMESPACE') {
@@ -169,13 +170,14 @@ private function getClassesList($file): array
169170
}
170171

171172
if ($tokens[$j]->getTokenName() === 'T_STRING') {
172-
$classes[] = $namespace . '\\' . $tokens[$j]->text;
173+
$classes[] = $namespace.'\\'.$tokens[$j]->text;
173174
} else {
174175
break;
175176
}
176177
}
177178
}
178179
}
180+
179181
return $classes;
180182
}
181183

src/LaravelLivewireTablesServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class LaravelLivewireTablesServiceProvider extends ServiceProvider
1414
public function boot(): void
1515
{
1616

17-
AboutCommand::add('Rappasoft Laravel Livewire Tables', fn () => ['Version' => '3.2.5']);
17+
AboutCommand::add('Rappasoft Laravel Livewire Tables', fn () => ['Version' => '3.2.6']);
1818

1919
$this->mergeConfigFrom(
2020
__DIR__.'/../config/livewire-tables.php', 'livewire-tables'

src/Views/Columns/DateColumn.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Columns;
44

5+
use Carbon\Carbon;
6+
use DateTime;
57
use Illuminate\Database\Eloquent\Model;
68
use Illuminate\Support\HtmlString;
79
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
@@ -28,25 +30,26 @@ public function getContents(Model $row): null|string|\BackedEnum|HtmlString|Data
2830
{
2931

3032
$dateTime = $this->getValue($row);
31-
if (! ($dateTime instanceof \DateTime)) {
32-
try {
33-
// Check if format matches what is expected
34-
if (! \Carbon\Carbon::hasFormatWithModifiers($dateTime, $this->getInputFormat())) {
35-
throw new \Exception('DateColumn Received Invalid Format');
36-
}
3733

38-
// Create DateTime Object
39-
$dateTime = \DateTime::createFromFormat($this->getInputFormat(), $dateTime);
40-
} catch (\Exception $exception) {
41-
report($exception);
34+
if ($dateTime != '' && $dateTime != null) {
35+
if ($dateTime instanceof DateTime) {
36+
return $dateTime->format($this->getOutputFormat());
37+
} else {
38+
try {
39+
// Check if format matches what is expected
40+
if (Carbon::canBeCreatedFromFormat($dateTime, $this->getInputFormat())) {
41+
return Carbon::createFromFormat($this->getInputFormat(), $dateTime)->format($this->getOutputFormat());
42+
}
43+
} catch (\Exception $exception) {
44+
report($exception);
45+
46+
// Return Null
47+
return $this->getEmptyValue();
48+
}
4249

43-
// Return Null
44-
return $this->getEmptyValue();
4550
}
4651
}
4752

48-
// Return
49-
return $dateTime->format($this->getOutputFormat());
50-
53+
return $this->getEmptyValue();
5154
}
5255
}

0 commit comments

Comments
 (0)