Skip to content

Instantly share code, notes, and snippets.

@mustafatoker
Last active April 4, 2018 07:33
Show Gist options
  • Save mustafatoker/bc8f5f93661fa0becf3ccad604c3776e to your computer and use it in GitHub Desktop.
Save mustafatoker/bc8f5f93661fa0becf3ccad604c3776e to your computer and use it in GitHub Desktop.
Laravel Blade to deal with null dates. Example; @Date($object->updated_at)
<?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