To define an accessor, create a method in your model getFooAttributewhere Foo is the name of the cropped name of the camel that you want to get. Thus, you must define an accessor for the attribute test_date(or something else). The accessor is automatically called by Eloquent when trying to get the value test_date.
(In the example, I define it in the user model)
<?php
namespace App;
use Carbon\Carbon;
class User extends Model {
protected $table = 'users';
public function getTestDateAttribute($date) {
$storedformat = createFromFormat('Y-m-d H:i:s', $date);
$customformat = $storedformat->format('Y.m.d. H:i:s');
return $customformat;
}
}
Then by default you will see the following format:
$user = User::find(1);
$user->test_date;
If you want to write simpler code, you should use Traits .Define the DateTrait file in the file App\DateTrait.php:
<?php
trait DateTrait {
public function getTestDateAttribute($date) {
$storedformat = createFromFormat('Y-m-d H:i:s', $date);
$customformat = $storedformat->format('Y.m.d. H:i:s');
return $customformat;
}
}
, test_date.
<?php
namespace App;
use Carbon\Carbon;
class User extends Model {
use App\DateTrait;
...
}