Quickly learn unfamiliar knowledge points in laravel
Laravel unfamiliar knowledge points
Default values of php parameters
<code><span><span><?php</span><span><span>function</span><span>makecoffee</span><span>(<span>$type</span> = <span>"cappuccino"</span>)</span> {</span><span>return</span><span>"Making a cup of $type.\n"</span>; } <span>echo</span> makecoffee(); <span>echo</span> makecoffee(<span>null</span>); <span>echo</span> makecoffee(<span>"espresso"</span>); <span>?></span></span></code>
Output
<code>Making <span>a</span> cup <span>of</span> cappuccino. Making <span>a</span> cup <span>of</span> . Making <span>a</span> cup <span>of</span> espresso.</code>
Model Binding (Model Binding)
In RouteServiceProvider, implement model binding in the boot method
<code><span>public</span> function boot(Router <span>$router</span>) { <span>parent</span><span>::boot</span>(<span>$router</span>); <span>$router</span><span>-></span>model(<span>'users'</span>, <span>'App\User'</span>); <span>$router</span><span>-></span>model(<span>'goods'</span>, <span>'App\Good'</span>); <span>$router</span><span>-></span>model(<span>'categories'</span>, <span>'App\Category'</span>); <span>$router</span><span>-></span>model(<span>'tryClothes'</span>, <span>'App\TryRecord'</span>); <span>$router</span><span>-></span>model(<span>'carts'</span>, <span>'App\Cart'</span>); <span>$router</span><span>-></span>model(<span>'orders'</span>, <span>'App\Order'</span>); <span>$router</span><span>-></span>model(<span>'orderItems'</span>, <span>'App\OrderItem'</span>); <span>//</span> }</code>
Form Request (Form Request)
- Use the following instructions to generate a custom Request
<code><span>php</span><span>artisan</span><span>make</span><span>:request</span><span>CreateArticleRequest</span></code>
- Customize the methods in Request: authorize() and rules(); authorize determines whether there is permission, and rules performs data verification
<code><span>public</span><span><span>function</span><span>authorize</span><span>()</span> {</span><span>return</span><span>true</span>; }</code>
<code><span>public</span><span><span>function</span><span>rules</span><span>()</span> {</span><span>return</span> [ <span>'title'</span> => <span>'required|min:3'</span>, <span>'body'</span> => <span>'required'</span>, <span>'published_at'</span> => <span>'required|date'</span>, <span>// 也可以使用数组</span><span>//'published_at' => ['required', 'date'],</span> ]; }</code>
- Use the Request method Usually POST data is passed in. The reason why the custom Request class is defined is to reuse code and decouple. You can use the Validate class to process the rules method in the custom Request
<code><span>public</span><span><span>function</span><span>store</span><span>(Request <span>$request</span>)</span>{</span><span>$this</span>->validate(<span>$request</span>, [<span>'title'</span> => <span>'required|min:3'</span>, <span>'body'</span> =><span>'required'</span>, <span>'published_at'</span> => <span>'required|date'</span>]); Article::create(<span>$request</span>->all()); <span>return</span> redirect(<span>'articles'</span>); }</code>
- If it passes the verification, you can use $request->all() directly sends data to the relevant class
<code>Article<span>::create</span>(<span>$request</span><span>-></span><span>all</span>());</code>
php storm laravel code tips
- https://gist.githubusercontent.com/barryvdh/5227822/raw/811f21a14875887635bb3733aef32da51fa0501e/_ide_helper. php
- Remember to add this file in the .gitignore file
Create the controller in a specific folder
<code>php artisan <span>make</span>:controller Console/ConsoleController</code>
- Note that there is no problem with the code written in routes.php, otherwise the following error will occur
<code><span>[</span>ReflectionException<span>]</span> Class App<span>\Http</span><span>\Controllers</span><span>\console</span> does not exist</code>
References
- Laravel 5.0 - Form Requests
- http://9ipp.com/web/laravel/laravel-5-form-request-controller-validation.html
- laracast
Copyright statement: This article is an original article by the blogger, No reproduction is allowed without the permission of the blogger.
The above introduces the quick learning of unfamiliar knowledge points in Laravel, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How to use the urllib.request.urlopen() function in Python3.x to send a GET request. In network programming, we often need to obtain data from a remote server by sending an HTTP request. In Python, we can use the urllib.request.urlopen() function in the urllib module to send an HTTP request and get the response returned by the server. This article will introduce how to use

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

After problems occurred in many centralized exchanges, more and more cryptocurrency investors began to transfer assets to cold wallets to reduce the risks posed by centralized exchanges. This article will introduce Trezor, the world's earliest cold wallet provider. Since the first cold wallet was launched in 2014, it has been sold in many countries around the world. Trezor's products include Model One launched in 2014 and the advanced version Model T launched in 2018. The following will continue to introduce the differences between these two products and other cold wallets. What is Trezor cold wallet? In 2014, Trezor launched the first cold wallet ModelOne. In addition to common BTC, ETH, USDT and other currencies, the wallet also supports more than 1,000 other currencies.
