File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Inventories/InventoryComposer.php
<?php
namespace App\Komma\Inventories;
use App\Komma\Inventories\Models\Inventory;
use App\Komma\PricingProducts\Models\PricingProduct;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\App;
class InventoryComposer
{
/**
* Bind data to the view.
*
* @param View $view
* @return View
*/
public function compose(View $view)
{
$packages = Inventory::with('translation')
->join('inventory_translations', 'inventories.id', '=', 'inventory_translations.inventory_id')
->select('inventories.*', 'inventory_translations.active', 'inventory_translations.language_id')
->where(function ($query) {
$query->where('active', '=', 1)
->where('language_id', '=', App::getLanguage()->id);
})
->with([
'pricingLabels' => function (BelongsToMany $query) {
$query->with('translation');
}, ])
->has('translation')
->get();
$masterArray = $this->generateMasterArray($packages);
$extraProducts = PricingProduct::whereIn('id', [7, 5, 3, 8])->with('translation')->get();
$coreProduct = PricingProduct::where('id', 6)->with('translation')->get();
return $view->with(['packages' => $packages, 'masterArray' => $masterArray, 'additionalProducts' => $extraProducts, 'coreProduct' => $coreProduct]);
}
/**
* @param $packages
* @return array
*
* Combine all package labels to one array with all unique labels
*/
public function generateMasterArray($packages)
{
$array = [];
foreach ($packages as $package) {
foreach ($package->pricingLabels as $label) {
if (! isset($label->translation)) {
continue;
}
$array[] = $label->translation->name;
}
}
return array_unique($array);
}
}