Home >PHP Framework >Laravel >Summary of useful functions of Laravel5.2 and laravel5.3 frameworks (with code)

Summary of useful functions of Laravel5.2 and laravel5.3 frameworks (with code)

不言
不言Original
2018-08-18 11:28:512353browse

This article brings you a summary of the useful functions of Laravel5.2 and laravel5.3 frameworks (with code). It has certain reference value. Friends in need can refer to it. , hope it helps you.

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: Through the return Pass three response headers X-RateLimit-Limit, X-RateLimit-Remaining, and Retry-After to 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 The 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 pass DB::table('users')->get()->all() Returning an array, but returning a collection also has certain benefits. We can use some methods of the collection to return the collection. 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

$loop Properties:

index: Loop index starting from 1

remaining: How many entries are left in the loop

count: Total number of loop entries

first: Whether Is the first

last: whether it is the last

depth: loop level

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

5. Super simple paging

Get data: User::paginate($num)
Template: $users->links()

May need to be introduced in the template css file, css file path public/css/app.css, you can directly00bddefe2d6dc164c385b183b45f18ed

The above is the entire content of this article For more laravel content, please pay attention to laravel framework introductory tutorial.

Related recommendations:

Summary of how to use the collection class in Laravel (code)

laravel framework model How to create and use model

The above is the detailed content of Summary of useful functions of Laravel5.2 and laravel5.3 frameworks (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn