Larave Validator verifies arrays and elements Author: Chuwen Time: 2021-08-30 Classification: Laravel , PHP comment ###Example ```php $rules = [ 'country_code_list []'=>['required_without: courier_code ',' array '],//This field must be an array 'country_code_list []. *'=>['string ',' distinct ',' size: 2 '],//The elements in the array must be strings, unique, and 2 in length ]; $validated = Validator::make($request->all(), $rules); //Print validation error messages dd($validated->errors()); ``` ###Simulate request data >POST request BODY ```json { "country_code_list[]": [ "CN", "JP" ] } ```
Naming rules for adding, modifying, and deleting fields of Laravel migration files Author: Chuwen Time: 2021-08-27 Classification: PHP 2 comments ##Personal specifications are for reference only ###New migration file >It is recommended to use the Create Model command to generate migration files ` php artisan make: model model name-m` ```shell php artisan make:model User -m #This will generate create_users_table #Table names with plural numbers will be automatically generated ``` ###Add Field >Rule: ` add_field name_to_table name` ```shell php artisan make:migration add_votes_to_users_table --table=users ``` ###Modify Field >Rule: ` change_field name_to_table name` ```shell php artisan make:migration change_votes_to_users_table --table=users ``` ###Delete Field >Rule: ` remove_field name_to_table name` ```shell php artisan make:migration remove_votes_to_users_table --table=users ```
Vue.js drop-down box component closes the menu when clicking outside the menu bar Author: Chuwen Time: 2021-08-23 Classification: Vue.js , JavaScript comment ## Menu.vue >Menu Components > ! []( https://cdn.nowtime.cc/2021/08/23/2193569241.png ) ``` <template> <div class="menu" v-if="show"> <!-- Suppose it is a menu component... --> </div> </template> <script> export default { data(){ return { Show: true,//Whether to display the menu } }, mounted() { //Just add it here document.addEventListener('click', (e) => { //If the DOM of the current component (menu component) "does not contain" the currently clicked DOM //Close the display menu if (!this.$el.contains(e.target)) this.show = false }) } } </script> ```
Determine which fields have been changed before/after saving the model Author: Chuwen Time: 2021-08-21 Classification: Other classifications comment ##Code example ```php $user = User::whereId(1); $user->email = 'i@nowtime.cc'; //Before submitting the update, obtain which fields have been modified $user->getDirty();// Return to ["email": "i @ nowtime. cc"] $user->save(); //After submitting the update, obtain which fields have been modified $user->getChanges();// Return to ["email": "i @ nowtime. cc"] ``` -'getDirty' called before updating -'getChanges' is called after updating
Larave should not return arrays in middleware, but use response to return Author: Chuwen Time: 2021-08-21 Classification: Laravel , PHP comment ##Larave should not return arrays in middleware, but use response to return Otherwise, errors like this will be reported: ```log { "message": "Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders(): Argument #1 ($response) must be of type Symfony\\Component\\HttpFoundation\\Response, array given, called in /vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequestsWithRedis.php on line 71", "exception": "TypeError", "file": "/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php", "line": 223, "trace": [ ] } ``` For details, please refer to: https://segmentfault.com/a/1190000038184631 --- <article class="article fmt article-content "> <p>I always want to write some articles on a regular basis, and always procrastinate< Br>Today, I'd like to share a very accidental exception I met when I was developing an API with larravel</ p><p><code>[2020-11-13 17:34:03] local. ERROR: Argument 1 passed to Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders() must be an instance of Symfony\\Component\\HttpFoundation\\Response, array given, called in /mnt/web/sp-api/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php on line 133 {"exception":"[object] (TypeError(code: 0) : Argument 1 passed to Illuminate\\Routing\\Middleware\\ThrottleRequests::addHeaders() must be an instance of Symfony\\Component\\HttpFoundation\\Response, array given, Called in/mnt/web/sp api/vendor/larravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests. php on line 133 at/mnt/web/sp api/vendor/lavel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests. php: 223)</ p> <p>This is a strange error. ThrottleRequests is the middleware used by larvel to limit flow. Why does an exception occur here</ p> <p>After breakpoint debugging, it is found that there is an API request signature verification middleware in the project. When the request verification fails, an error message will be returned< Br>The returned content is an array</ p><pre><code>/** *The request timestamp has expired */ Return ['state '=>false,' code '=>40001,' msg '=>' The request timestamp has expired ']</ Code></pre><p>This return is an irregular return, and does not return the response instance that the middleware should return< Br>In general, there is no problem, but after current limiting is triggered. If the injection object obtained by ThrottleRequests middleware is not a Response instance, an exception will be thrown. If the reason is found, it is easy to handle< Br>Let's modify the signature verification middleware, put the array to be returned into the response body, and then return a response instance through the response() method</p><pre><code>/** *The request timestamp has expired */ $response_data=return ['state '=>false,' code '=>40001,' msg '=>' The request timestamp has expired ']; return response($response_data);</ Code></pre><p>To solve the problem, we should pay attention not to return an array directly in the middleware, but to return a response instance. In this way, we will not encounter this weird problem, which is caused by the development not according to the specification</ p> </article>