Home > Article > Backend Development > 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)
<code><span>php</span><span>artisan</span><span>make</span><span>:request</span><span>CreateArticleRequest</span></code>
<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>
<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>
<code>Article<span>::create</span>(<span>$request</span><span>-></span><span>all</span>());</code>
php storm laravel code tips
Create the controller in a specific folder
<code>php artisan <span>make</span>:controller Console/ConsoleController</code>
<code><span>[</span>ReflectionException<span>]</span> Class App<span>\Http</span><span>\Controllers</span><span>\console</span> does not exist</code>
References
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.