定制 itsdamien/laravel-model-transformer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

itsdamien/laravel-model-transformer

最新稳定版本:v2.0.6

Composer 安装命令:

composer require itsdamien/laravel-model-transformer

包简介

A simple model transformer compatible with Laravel 5+.

README 文档

README

itsDamien Laravel Model Transformer

Latest Stable Version Total Downloads License Build Status Maintainability Test Coverage StyleCI

This package helps API developers to easily transform Eloquent models into collection that are convertible to JSON.

Installation

Installation using composer:

composer require itsdamien/laravel-model-transformer 

Usage

Create a model transformer class by extending the AbstractTransformer class:

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer { public function model($model) { return [ 'first_name' => $model->first_name, 'last_name' => $model->last_name, 'full_name' => $model->first_name.' '.$model->last_name, 'photos' => PhotoTransformer::transform($model->photos), ]; } }

Now you can call the transformer from any controller:

return response([ "user" => UserTransformer::transform(User::find(1)) ]); // Output: // { // "user":{ // "first_name":"John", // "last_name":"Doe", // "full_name":"John Doe", // "photos":[] // } // }

You can also pass a collection and the result will be an collection of transformed models:

return response([ "users" => UserTransformer::transform(User::all()) ]); // Output: // { // "users":[ // { // "first_name":"John", // "last_name":"Doe", // "full_name":"John Doe", // "photos":[] // }, // { // "first_name":"Dolores", // "last_name":"Abernathy", // "full_name":"Dolores Abernathy", // "photos":[] // }, // ] // }

Passing options to the transformer

You may need to pass some options from the controller to the transformer, you can do that by providing an array of options to the transform() method as a second parameter:

UserTransformer::transform($user, ['foo' => 'bar']);

Now from inside the UserTransformer you can check the options parameter:

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer { public function model($model) { return [ 'first_name' => $model->first_name, 'last_name' => $model->last_name, 'full_name' => $model->first_name.' '.$model->last_name, 'foo' => $this->options['foo'], ]; } }

Complex transformer

Your transformer will always transform your model with the model method. Then you can alter the transformer by adding your with or without methods to the transform() method as a third parameter:

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer { public function model($model) { return collect([ 'first_name' => $model->first_name, 'last_name' => $model->last_name, 'full_name' => $model->first_name.' '.$model->last_name, ]); } public function withId($model, \Illuminate\Support\Collection $collection) { return $collection->merge(collect([ 'id' => $model->id, ])); } public function withoutFullname($model, \Illuminate\Support\Collection $collection) { return $collection->except('full_name'); } }

Now call the transformer:

return UserTransformer::transform(User::find(1)); // Output: // { // "first_name":"John", // "last_name":"Doe", // "full_name":"John Doe" // }
return UserTransformer::transform(User::find(1), [], ['withId']); // Output: // { // "id":1, // "first_name":"John", // "last_name":"Doe", // "full_name":"John Doe" // }
return UserTransformer::transform(User::find(1), [], ['withoutFullname']); // Output: // { // "first_name":"John", // "last_name":"Doe", // }
return UserTransformer::transform(User::find(1), [], ['withId', 'withoutFullname']); // Output: // { // "id":1, // "first_name":"John", // "last_name":"Doe", // }

统计信息

  • 总下载量: 73.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固