Home > Article > Backend Development > Easily learn Laravel's form video tutorial
The Laravel framework is one of the most popular PHP development frameworks in the world. In recent years, Laravel has quickly occupied the top spot among PHP development frameworks due to its power, security, elegance and other features. Now the Laravel framework has become the preferred framework for large Internet companies and PHP siegeers.
Course playback address: http://www.php.cn/course/395.html
The Teacher's teaching style:
The teacher's lectures are simple and in-depth, clear in structure, analyzed layer by layer, interlocking, rigorous in argumentation, and rigorous in structure. He uses the logical power of thinking to attract students' attention and controls the classroom with reason. teaching process. The teaching skills are full of wit. Various teaching methods and techniques are readily available and can be used freely and appropriately without any trace of polishing.
The more difficult point in this video is Laravel-form list and paging implementation:
There is such a situation during the development process, you request the Java api to obtain information, because There is a lot of information and needs to be displayed in pages. Laravel officially provides a simple method, paginate($perPage), but this method only applies to model and query builder.
Today, let’s talk about how to achieve the same effect as the paginate method given an array.
View the source code of paginate method
#vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:480 public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null){ $query = $this->toBase(); $total = $query->getCountForPagination(); $this->forPage( $page = $page ?: Paginator::resolveCurrentPage($pageName), $perPage = $perPage ?: $this->model->getPerPage() ); return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); }
As you can see from the above, the key to pagination lies in LengthAwarePaginator.
Construction method of LengthAwarePaginator.
public function __construct($items, $total, $perPage, $currentPage = null, array $options = []){ foreach ($options as $key => $value) { $this->{$key} = $value; } $this->total = $total; $this->perPage = $perPage; $this->lastPage = (int) ceil($total / $perPage); $this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path; $this->currentPage = $this->setCurrentPage($currentPage, $this->lastPage); $this->items = $items instanceof Collection ? $items : Collection::make($items); }
In fact, we already understand it very well. If the array to be paged is
[ ['username'=>'zhangsan', 'age'=>26], ['username'=>'lisi', 'age'=>23], ['username'=>'wangwu', 'age'=>62], ['username'=>'zhaoliu', 'age'=>46], ['username'=>'wangmazi', 'age'=>25], ['username'=>'lanzi', 'age'=>24], ['username'=>'pangzi', 'age'=>21], ]
, there are 7 pieces of data in total, and each page displays 3 pieces, for a total of 3 pages.
use Illuminate\Pagination\LengthAwarePaginator;use Illuminate\Pagination\Paginator;use Illuminate\Http\Request; # 仅做演示 #function userList(Request $request) { $users = [ ['username'=>'zhangsan', 'age'=>26], ['username'=>'lisi', 'age'=>23], ['username'=>'wangwu', 'age'=>62], ['username'=>'zhaoliu', 'age'=>46], ['username'=>'wangmazi', 'age'=>25], ['username'=>'lanzi', 'age'=>24], ['username'=>'pangzi', 'age'=>21] ]; $perPage = 3; if ($request->has('page')) { $current_page = $request->input('page'); $current_page = $current_page <= 0 ? 1 :$current_page; } else { $current_page = 1; } $item = array_slice($users, ($current_page-1)*$perPage, $perPage); //注释1 $total = count($users); $paginator =new LengthAwarePaginator($item, $total, $perPage, $currentPage, [ 'path' => Paginator::resolveCurrentPath(), //注释2 'pageName' => 'page', ]); $userlist = $paginator->toArray()['data']; return view('userlist', compact('userlist', 'paginator')); }
The above code The key point is $item. If comment 1 is not processed, all 7 pieces of data will be obtained.
Note 2 is to set a url address to be paginated. It can also be set manually via $paginator ->setPath(‘path’).
The paging connection in the page is also called in the same way {{ $paginator->render() }}
The above is the detailed content of Easily learn Laravel's form video tutorial. For more information, please follow other related articles on the PHP Chinese website!