Skip to content

Instantly share code, notes, and snippets.

@nadinengland
Last active August 30, 2018 08:44
Show Gist options
  • Save nadinengland/0f65f75985b09d18354dcdb03b59e530 to your computer and use it in GitHub Desktop.
Save nadinengland/0f65f75985b09d18354dcdb03b59e530 to your computer and use it in GitHub Desktop.
How we simplified our development process: Code Snippets
<ul>
@foreach ($model['products'] as $product)
<li>{{ $product['name'] }}</li>
@endforeach
</ul>
<?php
abstract class Builder
{
/**
* Builds the array version on the input parameter.
*
* @param mixed $source
* @return mixed
*/
abstract public function buildOne($source);
/**
* Applies the `Builder#build` method to each element in the `$iterable`.
*
* @param iterable $iterable
* @return mixed[]
*/
public function buildAll(iterable $iterable)
{
return array_map([ $this, 'buildOne' ], $iterable);
}
}
@extends('app/product/index.blade.php’, [
'model' => [
'products' => [
[ 'name' => 'First' ],
[ 'name' => 'Second' ],
[ 'name' => 'Third' ],
[ 'name' => 'Forth' ],
],
],
])
php artisan vendor:publish --provider="Engage\LaravelFrontend\ServiceProvider"
<?php
class ProductBuilder extends Builder
{
/**
* Builds the array version on the input parameter.
*
* @param \App\Models\Product $product
* @return array
*/
public function buildOne($product)
{
return $product->only('name');
}
}
<?php
class ProductsController
{
/**
* Lists all Products.
*
* @param \App\Builders\ProductBuilder $builder
* @return \Illuminate\Http\Response
*/
public function index(ProductBuilder $builder)
{
$products = Product::all();
$model = [ 'products' => $builder>buildAll($products) ];
return view('products/index', compact('model'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment