1. Go to this page and download the library: Download pegziq/laravel-responder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pegziq / laravel-responder example snippets
public function index()
{
$resource = new Collection(User::all(), new UserTransformer());
return response()->json((new Manager)->createData($resource)->toArray());
}
public function index()
{
return responder()->success(User::all())->respond();
}
return responder()->success(Product::all(), new ProductTransformer)->respond();
class Product extends Model implements Transformable {}
/**
* Get a transformer for the class.
*
* @return \Flugg\Responder\Transformers\Transformer|string|callable
*/
public function transformer()
{
return ProductTransformer::class;
}
use Flugg\Responder\Transformers\TransformerResolver;
public function boot()
{
$this->app->make(TransformerResolver)->bind([
\App\Product::class => \App\Transformers\ProductTransformer::class,
\App\Shipment::class => \App\Transformers\ShipmentTransformer::class,
]);
}
namespace App\Transformers;
use App\User;
use Flugg\Responder\Transformers\Transformer;
class ProductTransformer extends Transformer
{
/**
* List of available relations.
*
* @var string[]
*/
protected $relations = ['*'];
/**
* A list of autoloaded default relations.
*
* @var array
*/
protected $load = [];
/**
* Transform the model.
*
* @param \App\Product $product
* @return array
*/
public function transform(Product $product): array
{
return [
'id' => (int) $product->id,
];
}
}
/**
* Include related shipments.
*
* @param \App\Product $product
* @param array|null $parameters
* @return \League\Fractal\ResourceInterface
*/
public function
return $this->resource($product->shipments, new ShipmentTransformer);
return responder()->error('sold_out_error', 'The requested product is sold out.')->respond();
return [
'sold_out_error' => 'The requested product is sold out.',
];
use Flugg\Responder\ErrorMessageResolver;
public function boot()
{
$this->app->make(ErrorMessageResolver::class)->register([
'sold_out_error' => 'The requested product is sold out.',
]);
}