xtremer360
1 month ago

Different Formats for Accessor

Posted 1 month ago by xtremer360

Currently the code below all works as expected, however, I was curious if anyone notices any possible flaws and what they might suggest doing differently.

I'll go through and explain what each class is doing.

I'm using Spatie's Model View package and put together a class to send through either a new manager or a specific instance. When its passing in a new instance then it will essentially pass in null for the the started_at property since we are creating a new model. If we are passing in a specific instance then we need to call the started_at accessor to see if the manager has any employments and if so then grab the first one if not then it should return null meaning we are editing a manager that was saved but didn't have an employment declared when originally creating the manager. When displaying the started_at property on the manager form I want it to display as a datetime string.

Below I also have a datatable class from using Yajra's laravel-datatables package. When I retrieve all managers with an employment and l am editing the startd_at field so that it can format how the field displays, I am displaying it as a date string without the time.

With having to use the optional helper method in multiple places is there a better way?

<?php

namespace App\ViewModels;

use App\Models\Manager;
use Spatie\ViewModels\ViewModel;

class ManagerViewModel extends ViewModel
{
    /** @var $manager */
    public $manager;

    /**
     * Undocumented function
     *
     * @param App\Models\Manager $manager
     */
    public function __construct(Manager $manager = null)
    {
        $this->manager = $manager ?? new Manager;
        $this->manager->started_at = optional($this->manager->started_at ?? null)->toDateTimeString();
    }
}
<?php

namespace App\DataTables;

use App\Models\Manager;
use App\Filters\ManagerFilters;
use Yajra\DataTables\Services\DataTable;

class ManagersDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables($query)
            ->editColumn('started_at', function (Manager $manager) {
                return $manager->started_at->toDateString();
            })
    }
}
<?php

namespace App\Models\Concerns;

use App\Models\Employment;

trait CanBeEmployed
{
    /**
     * Get all of the employments of the model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function employments()
    {
        return $this->morphMany(Employment::class, 'employable');
    }

    /**
     * Get the model's first employment date.
     *
     * @return string|null
     */
    public function getStartedAtAttribute()
    {
        return optional($this->employments->first())->started_at;
    }
}

Please sign in or create an account to participate in this conversation.