您的位置:首页 > 其它

钱包农场 API 开发手记 十一 订单

2018-04-09 00:00 260 查看

模型&数据库迁移&数据填充

php artisan make:model Order -m
php artisan migrate

php artisan make:seeder OrdersTableSeeder

public function run()
{
\App\Order::create([
'sn'    => str_random(10),
'product_id'    => 1,
'user_id'    => 1,
'price'    => 1.23,
'created_at' => new DateTime,
'updated_at' => new DateTime,
]);
}

php artisan db:seed --class=OrdersTableSeeder


修改数据表

php artisan make:migration add_type_to_orders_table --table=orders
php artisan make:migration change_addr_id_on_orders_table --table=orders
php artisan make:migration add_deleted_at_to_orders_table --table=orders
php artisan make:migration change_price_on_orders_table --table=orders
php artisan make:migration change_price_on_products_table --table=products
php artisan make:migration change_gold_num_on_user_datas_table --table=user_datas
php artisan make:migration add_stock_to_products_table --table=products
php artisan make:migration add_pay_status_to_orders_table --table=orders


策略

php artisan make:policy OrderPolicy --model=Order

public function update(User $user, Order $order)
{
return $user->isAuthorOf($order);
}

user.php

public function isAuthorOf($model)
{
return $this->id == $model->user_id;
}

AuthServiceProvider.php

protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Address::class => AddressPolicy::class,
];


路由

// 订单列表
$api->get('order', 'OrdersController@index')
->name('api.order.index');
// 订单购买
$api->post('order', 'OrdersController@store')
->name('api.order.store');
// 订单支付
$api->put('order', 'OrdersController@update')
->name('api.order.update');
// 订单删除
$api->delete('order', 'OrdersController@destroy')
->name('api.order.destroy');


控制器

touch app/Transformers/OrderTransformer.php
php artisan make:request Api/OrderRequest
php artisan make:controller Api/V1/OrdersController --resource
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: