钱包农场 API 开发手记 十 商品
2018-04-09 00:00
691 查看
模型&数据库迁移&模型工厂&数据填充
php artisan make:model Product -m php artisan migrate
php artisan make:factory ProductFactory
$factory->define(\App\Product::class, function (Faker $faker) { $date_time = $faker->date . ' ' . $faker->time; return [ 'sn' => str_random(10), 'img_url' => 'http://o8uz2td92.bkt.clouddn.com/2017-02-09_589c0fa247401.jpg', 'intro' => str_random(10), 'prompt' => str_random(10), 'service_tel_number' => '12345678', 'price' => '1.00', 'start_at' => $date_time, 'end_at' => $date_time, 'shelf_status' => 1, 'auc_status' => 0, 'created_at' => $date_time, 'updated_at' => $date_time, ]; });
php artisan make:seeder ProductsTableSeeder
public function run() { $products = factory(\App\Product::class)->times(100)->make(); \App\Product::insert($products->toArray()); }
database/seeds/DatabaseSeeder.php
Model::unguard(); $this->call(ProductsTableSeeder::class); Model::reguard();
php artisan db:seed
路由
// 竞拍列表 $api->get('product', 'ProductController@index') ->name('api.product.index');
控制器
touch app/Transformers/ProductTransformer.php php artisan make:request Api/ProductRequest php artisan make:controller Api/V1/ProductsController
class ProductsController extends Controller { /** * 竞拍列表 * * @return \Dingo\Api\Http\Response */ public function index() { $value = Cache::remember('product', 5, function () { return Product::where('shelf_status', 0)->get(); }); return $this->response->collection($value, ProductTransformer::class); } }
相关文章推荐
- 钱包农场 API 开发手记 三 微信登录
- 钱包农场 API 开发手记 十三 收货地址
- 钱包农场 API 开发手记 七 绑定手机
- 钱包农场 API 开发手记 六 更新用户信息
- 钱包农场 API 开发手记 二 SMS
- 钱包农场 API 开发手记 一 开篇
- 钱包农场 API 开发手记 十二 文章
- 钱包农场 API 开发手记 八 土豆
- 钱包农场 API 开发手记 十一 订单
- 钱包农场 API 开发手记 九 金豆
- 钱包农场 API 开发手记 四 JWT
- 淘宝API开发之商品类
- 关于上一篇日志《TWS API 开发手记——连接到TWS平台》的修正
- 猪悟能淘宝商品下载专家v3版开发手记
- WO+开放平台:API调用开发手记(话费计费接口2.0)
- WO+开放平台:API调用开发手记(话费计费接口2.0)
- WO+开放平台:API调用开发手记(关于签名字段)
- TWS API 开发手记——连接到TWS平台
- 微信小程序开发手记之六:API
- HapiJS开发手册