Blogger Information
Blog 64
fans 6
comment 2
visits 82824
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel--通用后台管理系统--实现数据库查询构造类的扩展--扩展DB类
王娇
Original
758 people have browsed it

学习总结

1.如果想扩展laravel中的类,必须先确定这个类是否包含macro()方法,如果包含这个静态方法,说明这个类可以扩展

2.必须继承ServiceProvider类,然后把扩展的方法写在boot()方法中

3.通过调用扩展类的macro()实现方法的扩展

4.需要在config/app.php中的providers属性中注册这个扩展文件类

1.在app/providers/新建一个DBServiceProvider.php的文件

  1. <?php
  2. namespace App\Providers;
  3. //DB类所在的命名空间,如果想扩展DB类,必须先引入DB类
  4. use Illuminate\Database\Query\Builder as QueryBuilder;
  5. use Illuminate\Support\ServiceProvider;
  6. class DBServiceProvider extends ServiceProvider
  7. {
  8. //把需要扩展的方法写在boot()方法中
  9. public function boot()
  10. {
  11. //macro('扩展方法名',function(){实现该方法})
  12. //扩展DB类中的查询结果集的方法,把结果集转换为数组
  13. QueryBuilder::macro('lists',function(){
  14. $data = $this->get()->toArray();
  15. foreach($data as $item):
  16. $res[]=(array)$item;
  17. endforeach;
  18. return $res;
  19. });
  20. //扩展DB类中的查询结果为一条记录,把结果转换为数组
  21. QueryBuilder::macro('item',function(){
  22. $data = $this->get()->first();
  23. $data = (array)$data;
  24. return $data;
  25. });
  26. //扩展DB类中的查询结果为数组的索引转换为记录的索引
  27. //也可以传递参数
  28. QueryBuilder::macro('cate',function($index){
  29. $data = $this->lists();
  30. foreach($data as $item):
  31. //把查询结果集中的索引转换为记录中索引号
  32. $res[$item[$index]]=(array)$item;
  33. endforeach;
  34. return $res;
  35. });
  36. }
  37. }

2.注册数据库扩展方法config/app.php中的providers属性中注册该扩展方法

  1. <?php
  2. return [
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Application Name
  6. |--------------------------------------------------------------------------
  7. |
  8. | This value is the name of your application. This value is used when the
  9. | framework needs to place the application's name in a notification or
  10. | any other location as required by the application or its packages.
  11. |
  12. */
  13. 'name' => env('APP_NAME', 'Laravel'),
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Application Environment
  17. |--------------------------------------------------------------------------
  18. |
  19. | This value determines the "environment" your application is currently
  20. | running in. This may determine how you prefer to configure various
  21. | services the application utilizes. Set this in your ".env" file.
  22. |
  23. */
  24. 'env' => env('APP_ENV', 'production'),
  25. /*
  26. |--------------------------------------------------------------------------
  27. | Application Debug Mode
  28. |--------------------------------------------------------------------------
  29. |
  30. | When your application is in debug mode, detailed error messages with
  31. | stack traces will be shown on every error that occurs within your
  32. | application. If disabled, a simple generic error page is shown.
  33. |
  34. */
  35. 'debug' => (bool) env('APP_DEBUG', false),
  36. /*
  37. |--------------------------------------------------------------------------
  38. | Application URL
  39. |--------------------------------------------------------------------------
  40. |
  41. | This URL is used by the console to properly generate URLs when using
  42. | the Artisan command line tool. You should set this to the root of
  43. | your application so that it is used when running Artisan tasks.
  44. |
  45. */
  46. 'url' => env('APP_URL', 'http://localhost'),
  47. 'asset_url' => env('ASSET_URL', null),
  48. /*
  49. |--------------------------------------------------------------------------
  50. | Application Timezone
  51. |--------------------------------------------------------------------------
  52. |
  53. | Here you may specify the default timezone for your application, which
  54. | will be used by the PHP date and date-time functions. We have gone
  55. | ahead and set this to a sensible default for you out of the box.
  56. |
  57. */
  58. 'timezone' => 'Asia/Shanghai',
  59. /*
  60. |--------------------------------------------------------------------------
  61. | Application Locale Configuration
  62. |--------------------------------------------------------------------------
  63. |
  64. | The application locale determines the default locale that will be used
  65. | by the translation service provider. You are free to set this value
  66. | to any of the locales which will be supported by the application.
  67. |
  68. */
  69. 'locale' => 'en',
  70. /*
  71. |--------------------------------------------------------------------------
  72. | Application Fallback Locale
  73. |--------------------------------------------------------------------------
  74. |
  75. | The fallback locale determines the locale to use when the current one
  76. | is not available. You may change the value to correspond to any of
  77. | the language folders that are provided through your application.
  78. |
  79. */
  80. 'fallback_locale' => 'en',
  81. /*
  82. |--------------------------------------------------------------------------
  83. | Faker Locale
  84. |--------------------------------------------------------------------------
  85. |
  86. | This locale will be used by the Faker PHP library when generating fake
  87. | data for your database seeds. For example, this will be used to get
  88. | localized telephone numbers, street address information and more.
  89. |
  90. */
  91. 'faker_locale' => 'en_US',
  92. /*
  93. |--------------------------------------------------------------------------
  94. | Encryption Key
  95. |--------------------------------------------------------------------------
  96. |
  97. | This key is used by the Illuminate encrypter service and should be set
  98. | to a random, 32 character string, otherwise these encrypted strings
  99. | will not be safe. Please do this before deploying an application!
  100. |
  101. */
  102. 'key' => env('APP_KEY'),
  103. 'cipher' => 'AES-256-CBC',
  104. /*
  105. |--------------------------------------------------------------------------
  106. | Autoloaded Service Providers
  107. |--------------------------------------------------------------------------
  108. |
  109. | The service providers listed here will be automatically loaded on the
  110. | request to your application. Feel free to add your own services to
  111. | this array to grant expanded functionality to your applications.
  112. |
  113. */
  114. 'providers' => [
  115. /*
  116. * Laravel Framework Service Providers...
  117. */
  118. Illuminate\Auth\AuthServiceProvider::class,
  119. Illuminate\Broadcasting\BroadcastServiceProvider::class,
  120. Illuminate\Bus\BusServiceProvider::class,
  121. Illuminate\Cache\CacheServiceProvider::class,
  122. Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
  123. Illuminate\Cookie\CookieServiceProvider::class,
  124. Illuminate\Database\DatabaseServiceProvider::class,
  125. Illuminate\Encryption\EncryptionServiceProvider::class,
  126. Illuminate\Filesystem\FilesystemServiceProvider::class,
  127. Illuminate\Foundation\Providers\FoundationServiceProvider::class,
  128. Illuminate\Hashing\HashServiceProvider::class,
  129. Illuminate\Mail\MailServiceProvider::class,
  130. Illuminate\Notifications\NotificationServiceProvider::class,
  131. Illuminate\Pagination\PaginationServiceProvider::class,
  132. Illuminate\Pipeline\PipelineServiceProvider::class,
  133. Illuminate\Queue\QueueServiceProvider::class,
  134. Illuminate\Redis\RedisServiceProvider::class,
  135. Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
  136. Illuminate\Session\SessionServiceProvider::class,
  137. Illuminate\Translation\TranslationServiceProvider::class,
  138. Illuminate\Validation\ValidationServiceProvider::class,
  139. Illuminate\View\ViewServiceProvider::class,
  140. /*
  141. * Package Service Providers...
  142. */
  143. /*
  144. * Application Service Providers...
  145. */
  146. App\Providers\AppServiceProvider::class,
  147. App\Providers\AuthServiceProvider::class,
  148. // App\Providers\BroadcastServiceProvider::class,
  149. App\Providers\EventServiceProvider::class,
  150. App\Providers\RouteServiceProvider::class,
  151. //**************************************
  152. //扩展DBServiceProvider类
  153. App\Providers\DBServiceProvider::class,
  154. //******************************************
  155. ],
  156. /*
  157. |--------------------------------------------------------------------------
  158. | Class Aliases
  159. |--------------------------------------------------------------------------
  160. |
  161. | This array of class aliases will be registered when this application
  162. | is started. However, feel free to register as many as you wish as
  163. | the aliases are "lazy" loaded so they don't hinder performance.
  164. |
  165. */
  166. 'aliases' => [
  167. 'App' => Illuminate\Support\Facades\App::class,
  168. 'Arr' => Illuminate\Support\Arr::class,
  169. 'Artisan' => Illuminate\Support\Facades\Artisan::class,
  170. 'Auth' => Illuminate\Support\Facades\Auth::class,
  171. 'Blade' => Illuminate\Support\Facades\Blade::class,
  172. 'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
  173. 'Bus' => Illuminate\Support\Facades\Bus::class,
  174. 'Cache' => Illuminate\Support\Facades\Cache::class,
  175. 'Config' => Illuminate\Support\Facades\Config::class,
  176. 'Cookie' => Illuminate\Support\Facades\Cookie::class,
  177. 'Crypt' => Illuminate\Support\Facades\Crypt::class,
  178. 'DB' => Illuminate\Support\Facades\DB::class,
  179. 'Eloquent' => Illuminate\Database\Eloquent\Model::class,
  180. 'Event' => Illuminate\Support\Facades\Event::class,
  181. 'File' => Illuminate\Support\Facades\File::class,
  182. 'Gate' => Illuminate\Support\Facades\Gate::class,
  183. 'Hash' => Illuminate\Support\Facades\Hash::class,
  184. 'Http' => Illuminate\Support\Facades\Http::class,
  185. 'Lang' => Illuminate\Support\Facades\Lang::class,
  186. 'Log' => Illuminate\Support\Facades\Log::class,
  187. 'Mail' => Illuminate\Support\Facades\Mail::class,
  188. 'Notification' => Illuminate\Support\Facades\Notification::class,
  189. 'Password' => Illuminate\Support\Facades\Password::class,
  190. 'Queue' => Illuminate\Support\Facades\Queue::class,
  191. 'Redirect' => Illuminate\Support\Facades\Redirect::class,
  192. 'Redis' => Illuminate\Support\Facades\Redis::class,
  193. 'Request' => Illuminate\Support\Facades\Request::class,
  194. 'Response' => Illuminate\Support\Facades\Response::class,
  195. 'Route' => Illuminate\Support\Facades\Route::class,
  196. 'Schema' => Illuminate\Support\Facades\Schema::class,
  197. 'Session' => Illuminate\Support\Facades\Session::class,
  198. 'Storage' => Illuminate\Support\Facades\Storage::class,
  199. 'Str' => Illuminate\Support\Str::class,
  200. 'URL' => Illuminate\Support\Facades\URL::class,
  201. 'Validator' => Illuminate\Support\Facades\Validator::class,
  202. 'View' => Illuminate\Support\Facades\View::class,
  203. ],
  204. ];
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:作业尽可能不要集中在双休日提交
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post