Home  >  Article  >  PHP Framework  >  An introduction to Laravel’s practical little functions

An introduction to Laravel’s practical little functions

藏色散人
藏色散人forward
2020-04-03 09:04:103261browse

1. Control the number of accesses

The new feature of laravel5.2, set throttle through middleware to control the number of accesses based on IP

Principle: By returning three The response headers X-RateLimit-Limit, X-RateLimit-Remaining, and Retry-After control the number of accesses.

X-RateLimit-Limit: The maximum number of requests allowed within the specified time

X-RateLimit-Remaining: The remaining number of requests within the specified time

Retry-After: The distance below Time to wait for retry requests (s)

Code implementation:

// 一分钟内同一个IP限制访问5次
Route::group(['prefix' => 'admin', 'middleware' => 'throttle:5'], function(){
    Route::get('user', 'UserController@show');
});

2. A magical command to realize login registration

laravel5 .2 new features

php artisan make:auth

3.all()

laravel5.3 new features

laravel5.2: DB::table('users')->get() returns an array.

laravel5.3: DB::table('users')->get() returns a collection.

If we are using laravel5.3, we can return an array through DB::table('users')->get()->all(), but returning a collection also has certain benefits. Return We can use some methods of collections. For example, to take out the first element in the collection, we can directly use the first() method.

4.$loop

laravel5.3 new features

$loop variable is used in the @foreach loop

$ Properties provided by loop:

index: Loop index starting from 1

remaining: How many entries are left in the loop

count: Total number of entries in the loop

first: whether it is the first one

last: whether it is the last one

depth: loop level

parent: if the loop is in another @foreach, return the parent loop reference

5. Super simple paging

Get data: User::paginate($num)

Template: $users->links( )

You may need to introduce a css file into the template. The css file path is public/css/app.css. You can directly00bddefe2d6dc164c385b183b45f18ed

Recommended: laravel tutorial

The above is the detailed content of An introduction to Laravel’s practical little functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete