Closed jpmaga closed 4 years ago
Can you explain a little bit about when you'd use this?
Can you explain a little bit about when you'd use this?
Sure. Most recently I was working with an old codebase and the blade templates for some pages had variables already defined as $title, $testimonials, $gallery, ... and they were all coming straight from an Eloquent Model. I did want to refactor the controllers, but not really the templates. I also didn't want to define the view model with something like this:
public function __construct() {
$this->page = Page::find(1);
$this->title = $this-page->title;
$this->testimonials = $this-page->testimonials;
$this->gallery = $this-page->gallery;
// ...
}
Seemed much easier to do it like this:
protected $destructure = ['page'];
public function page() {
return Page::find(1);
}
(sorry about the syntax in the example, wrote this on mobile)
So by destructuring 'page'
, it would make title
, testimonials
and gallery
directly available as variables on the viewmodel, is that correct?
So by destructuring
'page'
, it would maketitle
,testimonials
andgallery
directly available as variables on the viewmodel, is that correct?
Yes, that is correct.
I'm not sure if we want to add this feature. I realise it might be useful in some cases, like you said you didn't want to change the view, but in my opinion the correct usage would be to use {{ $page->title }}
in the view, or explicitly define the data you want to expose in the view model.
I realise there might be cases where this is useful, but I don't think we should add this functionality to the package for these edge exceptional situations. Luckily the base view model is extensible, so you're free to add this functionality in your own ViewModel base class in a project when you really need it.
So while I appreciate your effort to help improve this package, I'm not going to merge this PR.
This has come in handy more than once, so figured it might be useful to others as well.