Skip to content

Instantly share code, notes, and snippets.

@cartoonclouds
Last active January 15, 2020 04:19
Show Gist options
  • Save cartoonclouds/3444f036e964fa0f201927e0036ccad0 to your computer and use it in GitHub Desktop.
Save cartoonclouds/3444f036e964fa0f201927e0036ccad0 to your computer and use it in GitHub Desktop.
View Models/Presenters
public function viewItem(Item $item)
{
$data['product'] = \App\Presenters\VuePresenters\ItemPresenter::decorate($product);
return view('item', $data);
}
// https://laravel-news.com/laravel-view-models
// https://github.com/spatie/laravel-view-models
// http://niceprogrammer.com/laravel-view-model/
<?php
namespace App\Presenters\VuePresenters;
use App\Presenters\Presenter;
use Item;
class ItemPresenter extends Presenter
{
protected $appends = [
'additionalCats',
'allowAddToCart',
'appliedPromotion',
'arrivingSoon',
'categoryPath',
'inTransit',
'isInStock',
'productBundleMiniDetails',
'productDamagedItems',
'productEndOfCodeItems',
'productShipmentDetails',
'salesHistory',
'showPreorder',
'specialPricingAvailable',
'stockAvailable',
'stockDetailsStaff',
];
protected $with = [
'tieredPrices'
];
protected function __construct(Item $model)
{
parent::__construct($model);
$this->model->load(['format', 'primaryCategory', 'categories', 'primarySupplier', 'primarySupplier.currency', 'tieredPrices']);
}
public static function decorate(Item $model)
{
return new self($model);
}
public function getAppliedPromotionAttribute()
{
return $this->model->appliedPromotion();
}
public function getAdditionalCatsAttribute()
{
return $this->model->categories()->where('id', '<>', $this->model->primaryCategory ? $this->model->primaryCategory->id : null)->get()
->each(function(\Category $category) {
$category->append('fullname');
});
}
public function getIsInStockAttribute()
{
return $this->model->isInStock();
}
public function getSpecialPricingAvailableAttribute()
{
return $this->model->specialPricingAvailable();
}
public function getArrivingSoonAttribute()
{
return $this->model->arrivingSoon();
}
public function getInTransitAttribute()
{
return $this->model->inTransit();
}
public function getStockAvailableAttribute()
{
return $this->model->stockAvailable();
}
public function getAllowAddToCartAttribute()
{
return $this->model->allowAddToCart();
}
public function getShowPreorderAttribute()
{
$this->model->showPreorderLabel();
}
public function getProductBundleMiniDetailsAttribute()
{
return view('products.templates.productBundleMiniDetails', ['item' => $this->model])->render();
}
public function getProductShipmentDetailsAttribute()
{
return view('products.templates.productShipmentDetails', ['item' => $this->model, 'backordered' => $this->model->public_soh])->render();
}
public function getSalesHistoryAttribute()
{
return view('products.templates.salesHistory', ['item' => $this->model])->render();
}
public function getProductDamagedItemsAttribute()
{
return view('products.templates.productDamagedItems', ['item' => $this->model])->render();
}
public function getProductEndOfCodeItemsAttribute()
{
return view('products.templates.productEndOfCodeItems', ['item' => $this->model])->render();
}
public function getStockDetailsStaffAttribute()
{
return $this->model->stockDetailsStaff()->render();
}
public function getCategoryPathAttribute()
{
return collect($this->model->getCategoryHierarchy(true))->map(function ($category) {
return "<a href='$category->url'>$category->name</a>";
})->implode(' <span class="fa fa-angle-right"></span> ');
}
}
<?php
namespace App\Presenters;
abstract class Presenter extends \Eloquent
{
protected $model;
public function __construct(\Eloquent $model)
{
$this->model = $model;
}
public function __call($method, $args)
{
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $args);
}
return call_user_func_array([$this->model, $method], $args);
}
public function __get($name)
{
return $this->model->getAttribute( $name ) ?? $this->getAttribute( $name );
}
/**
* Convert the presenter and model instance to an array.
*
* @return array
*/
public function toArray()
{
return array_merge($this->model->toArray(), $this->attributesToArray(), $this->relationsToArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment