spatie / laravel-view-models

View models in Laravel
https://spatie.be/open-source
MIT License
1.02k stars 61 forks source link

Fix for Data Property duplication #41

Closed JamesFreeman closed 2 years ago

JamesFreeman commented 2 years ago

Quick bug fix for the following issue: https://github.com/spatie/laravel-view-models/pull/39#issuecomment-1011069714

asurcodes commented 2 years ago

This issue can be solved by creating a temporary variable and then ignoring it but it feels hacky, I totally support this chage:

<?php

namespace App\Api\ViewModels;

use Spatie\ViewModels\ViewModel;

class BaseViewModel extends ViewModel
{
    protected $ignore = ['content'];

    public $content;

    public function message(): string
    {
        return __('models.base.message');
    }

    public function data(): array
    {
        return $this->content->toArray();
    }
}

Thanks for the blazing fast response to my comment BTW! 👏🏼

brendt commented 2 years ago

Good solution! Thanks, I'll tag it right now

brendt commented 2 years ago

Tagged: https://github.com/spatie/laravel-view-models/releases/tag/1.5.1