首页天道酬勤Laravel api,api接口文档管理工具

Laravel api,api接口文档管理工具

张世龙 05-06 00:13 61次浏览

正文目录1、地址管理1.1地址模型的建立,迁移文件1.2地址验证类1.3地址的建立transform地址资源API控制器1.4地址路由1.5订单地址的修改1.6测试效果

一、地址管理1.1地址模型建立和文件迁移

运行phpartisanmake 3360 model address-m命令

在迁移文件中创建表结构:

方案:3360创建(地址),功能) blueprint$table ) { $table-id; $table-integer(user_id )-comment ) (用户id ); $table-string('name )-comment ) (收割者); $table-integer(city_id )-comment ) (city表的id ); $ table-string (地址)-comment ()详细地址); $table-string('phone ' )-comment ()、手机号码); $table-tinyinteger(is_default )-default(0) (0)-comment ) (默认地址0=不是默认地址,1=默认) ); $table-timestamps (; $table-index(user_id ); //查询索引};

运行php artisan migrate命令:

1.2创建地址验证类命令phpartisanmake :请求web/address请求:

写入:

publicfunctionrules((return )、function )、required (city _ id ) )、function (attribute )、$value if(empty ) $ ccid ,' address'='required ',' phone '=' required|regex :/^1[3-9]\d {9} $/',]; } /** *提示消息*/publicfunction messages ((return (' name.required )='收件人必填),' city_id.required'='地址为}

1.3地址设置创建变换地址资源的API控制器在模型中设置可批量插入的字段。

用户硬件工厂; protected $fillable=['user_id '、' name '、' phone '、' address '、' city_id '、' is_default'];

在City.php模型中写入查找父级的联动:

/** *查找父类*/public function parent () return $ this-belongs to (city 33603360 class,' pid ',' id ' )。 }

在辅助函数中写入根据区域id检索上位联动数据:

/**区ID下完整的省市区数据*/if (! function_exists(city_name ) ) functioncity_name ) $city_id ) $city=city3360:where )、$ city=city 3360 ? ' '; //$str .=$

city['parent']['parent']['name'] ?? ''; // $str .= $city['parent']['name'] ?? ''; // $str .= $city['name'] ?? ''; $str = [ $city['parent']['parent']['parent']['name'] ?? '', $city['parent']['parent']['name'] ?? '', $city['parent']['name'] ?? '', $city['name'] ?? '' ]; return trim(implode(' ', $str)); } }

创建AddressTransformer.php写入:

<?php namespace App\Transformers;use App\Models\Address;use League\Fractal\TransformerAbstract;class AddressTransformer extends TransformerAbstract { public function transform(Address $address) { return [ 'id' => $address->id, 'name' => $address->name, 'city_id' => $address->city_id, 'city_name' => city_name($address->city_id), 'phone' => $address->phone, 'address' => $address->address, 'is_default' => $address->is_default, 'created_at' => $address->created_at, 'updated_at' => $address->updated_at, ]; }}

运行命令php artisan make:controller Web/AddressController --api

写入增删改查以及是否默认方法:

<?phpnamespace App\Http\Controllers\Web;use App\Http\Controllers\BaseController;use App\Http\Requests\Web\AddressRequest;use App\Models\Address;use App\Transformers\AddressTransformer;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;class AddressController extends BaseController{ /** * 我的地址列表 */ public function index() { $address = Address::where('user_id', auth('api')->id())->get(); return $this->response->collection($address, new AddressTransformer()); } /** * 添加地址 */ public function store(AddressRequest $request) { Address::create([ 'user_id' => auth('api')->id(), 'name' => $request->input('name'), 'phone' => $request->input('phone'), 'address' => $request->input('address'), 'city_id' => $request->input('city_id'), ]); return $this->response->created(); } /** * 地址详情 */ public function show(Address $address) { return $this->response->item($address, new AddressTransformer()); } /** * 更新地址 */ public function update(AddressRequest $request, Address $address) { $address->update([ 'user_id' => auth('api')->id(), 'name' => $request->input('name'), 'phone' => $request->input('phone'), 'address' => $request->input('address'), 'city_id' => $request->input('city_id'), 'is_default' => $request->input('is_default') ]); return $this->response->noContent(); } /** * 删除地址 */ public function destroy(Address $address) { $address->delete(); return $this->response->noContent(); } /** * 默认地址 */ public function isDefault(Address $address) { if ($address->is_default == 1) { return $this->response->noContent(); // 如果改地址已经设置默认地址 直接返回 } try { DB::beginTransaction(); // 开启事物, 防止一个设置成功一个设置失败 // 先把所有的地址都设置为非默认 $default_address = Address::where('user_id', auth('api')->id()) ->where('is_default', 1) ->first(); if (!empty($default_address)) { $default_address->is_default = 0; $default_address->save(); } // 在把当前的设置成默认 $address->is_default = 1; $address->save(); DB::commit(); return $this->response->noContent(); } catch (\Exception $e) { DB::rollback(); throw $e; } }} 1.4 配置地址路由 // 地址增删改查 $api->resource('address', AddressController::class); // 设置默认地址 $api->patch('address/{address}/default', [AddressController::class, 'isDefault']);

1.5 订单地址修改

由于之前没有写地址相关的api,地址那块是模拟的假数据,现在把它修改过来:

// 地址数据 $address = Address::where('user_id', auth('api')->id()) ->orderBy('is_default', 'desc') ->get();

还有一处地方就是在提交订单时候验证地址是否存在:

'address_id' => 'required|exixts:addresses,id'

1.6 测试效果

1、添加地址

2、地址列表

3、修改地址

4、地址详情

5、删除地址

6、设置默认地址

在学习的php的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。

大学跨境电商物流管理试题,电商物流怎么做