---
# System prepended metadata

title: laravel macros
tags: [Laravel]

---

# laravel macros

https://www.youtube.com/watch?v=XQefAcs4qf0
## 介紹
擴張class方法
laravel有這個
![](https://i.imgur.com/x8kk2AC.png)
利用_call方法
可以自訂語法

ex
Macro excel之類的套件就也有支持
![](https://i.imgur.com/lRA0hRK.png)
![](https://i.imgur.com/iWJR2vY.png)



## 舉例
Ex
旅人的collection pagint

```
  //分頁自定義
        /**
         * Paginate a standard Laravel Collection.
         *
         * @param int $perPage
         * @param int $total
         * @param int $page
         * @param string $pageName
         * @return array
         */
        Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') {
            $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

            return new LengthAwarePaginator(
                $this->forPage($page, $perPage),
                $total ?: $this->count(),
                $perPage,
                $page,
                [
                    'path' => LengthAwarePaginator::resolveCurrentPath(),
                    'pageName' => $pageName,
                ]
            );
        });
```


excel的


###### tags: `Laravel`