Last active
April 4, 2018 07:33
-
-
Save mustafatoker/bc8f5f93661fa0becf3ccad604c3776e to your computer and use it in GitHub Desktop.
Laravel Blade to deal with null dates. Example; @Date($object->updated_at)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\Facades\Blade; | |
use Illuminate\Support\ServiceProvider; | |
class BladeServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Indicates if loading of the provider is deferred. | |
* | |
* @var bool | |
*/ | |
protected $defer = false; | |
/** | |
* Register any misc. blade extensions. | |
*/ | |
public function register() | |
{ | |
/** | |
* @date blade directive | |
* use as @date($object->datefield) | |
* or with a format @date($object->datefield,'m/d/Y') | |
*/ | |
Blade::directive('date', function ($expression) { | |
$default = "'d-m-Y H:i'"; | |
$parts = str_getcsv($expression); | |
$parts[1] = (isset($parts[1]))?$parts[1]:$default; | |
return '<?php if(' . $parts[0] . '){ echo e(' . $parts[0] . '->format(' . $parts[1] . ')); } ?>'; | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment