<?php //控制器 namespace AppHttpControllersApiUser; use AppHttpControllersController; use IlluminateHttpRequest; use AppModelUser as UserModel; // 设备区域 class DeviceRegionController extends Controller { public function index(Request $request) { $token = $request->input('token'); $limit = $request->input('limit'); $deviceRegionList = (new UserModel)->where('token', $token)->first()->device_region()->paginate($limit)->toArray(); $returnData = []; $returnData['msg'] = "查询成功"; $returnData['count'] = $deviceRegionList['total']; $returnData['current_page'] = $deviceRegionList['current_page']; $returnData['data'] = $deviceRegionList['data']; return success($returnData); } }
<?php // 模型 namespace AppModel; use IlluminateDatabaseEloquentModel; class User extends Model { // 指定表名 // laravel自动会+s protected $table = 'user'; // 关联设备区域表 public function device_region() { // 需要存在 DeviceRegion 模型才可以引入,并且需要与User模型在同一目录下 return $this->hasMany(DeviceRegion::class); }