亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

在Laravel外使用Eloquent(二)- 分頁(yè)問(wèn)題

junfeng777 / 1800人閱讀

摘要:在上一篇在外使用一中我們演示了如何引入以及基本使用,但是如果細(xì)心的朋友肯定會(huì)發(fā)現(xiàn),當(dāng)你在使用來(lái)分頁(yè)的時(shí)候是會(huì)報(bào)錯(cuò)的。但是引入那個(gè)模塊同時(shí)它內(nèi)部依賴(lài)了的模塊,意味著為了一個(gè)分頁(yè)功能我們要裝好多東西。

在上一篇《在Laravel外使用Eloquent(一)》 中我們演示了如何引入Eloquent以及基本使用,但是如果細(xì)心的朋友肯定會(huì)發(fā)現(xiàn),當(dāng)你在使用paginate(15)來(lái)分頁(yè)的時(shí)候是會(huì)報(bào)錯(cuò)的。因?yàn)槲覀儧](méi)有依賴(lài)laravel的pagination模塊。但是引入那個(gè)模塊同時(shí)它內(nèi)部依賴(lài)了symfony的http-foundation模塊,意味著為了一個(gè)分頁(yè)功能我們要裝好多東西。于是我就實(shí)現(xiàn)了一個(gè)比較簡(jiǎn)單的分頁(yè)類(lèi):

代碼見(jiàn):https://github.com/overtrue/rester

php
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author overtrue 
 * @github https://github.com/overtrue
 * @url    http://overtrue.me
 * @date   2014-10-23T20:05:33
 */

use Closure;
use Countable;
use ArrayAccess;
use Serializable;
use ArrayIterator;
use JsonSerializable;
use IteratorAggregate;

class Paginator implements
    ArrayAccess,
    Countable,
    IteratorAggregate,
    Serializable,
    JsonSerializable
{
    protected $pager;
    protected $pageSize;
    protected $total;
    protected $items;

    /**
     * Constructor
     *
     * @param string             $pager
     */
    public function __construct($pager = "page")
    {
        $this->pager = $pager;
    }

    /**
     * Make a pagination
     *
     * @param array   $items
     * @param integer $total
     * @param integer $pageSize
     *
     * @return array
     */
    public function make($items, $total, $pageSize = 10)
    {
        $this->total    = abs($total);
        $this->pageSize = $pageSize;
        $this->items = $items;

        return $this;
    }

    /**
     * Return current page
     *
     * @return integer
     */
    public function getCurrentPage($total = null)
    {
        $page = abs(app()->request->get("page", 1));

        if ($total) {
            $this->total = $total;
        }

        $page >= 1 || $page = 1;

        if ($this->items) {
            $totalPage = $this->getTotalPage();
            $page <= $totalPage || $page = $totalPage;
        }

        return $page;
    }

    /**
     * Return total pages
     *
     * @return integer
     */
    public function getTotalPage()
    {
        $this->pageSize > 0 || $this->pageSize = 10;

        $totalPage = ceil($this->total / $this->pageSize);

        $totalPage >= 1 || $totalPage = 1;

        return $totalPage;
    }

    public function links()
    {
        $html = "
    "; $totalPage = $this->getTotalPage(); $currentPage = $this->getCurrentPage(); if ($totalPage < 10) { for ($i = 1; $i <= $totalPage; $i++) { $active = $i == $currentPage ? "class="active"":""; $html .= "
  • $i
  • "; } } else { if ($currentPage > 3) { $html .= "
  • «
  • "; $start = $currentPage - 2; } else { $start = 1; } for ($i = $start; $i <= $currentPage; $i++) { $active = $i == $currentPage ? "class="active"":""; $html .= "
  • $i
  • "; } for ($i = $currentPage + 1; $i <= $currentPage + 3; $i++) { $active = $i == $currentPage ? "class="active"":""; $html .= "
  • $i
  • "; } if ($totalPage - $currentPage >= 5) { $html .= "
  • ...
  • "; $html .= "
  • $totalPage
  • "; } } return $html .= "
"; } /** * getLink * * @param integer $page * * @return string */ public function getLink($page) { static $query; if (is_null($query)) { $query = app()->request->get(); } $query["page"] = $page; return "?" . http_build_query($query); } /** {@inhertDoc} */ public function jsonSerialize() { return $this->items; } /** {@inhertDoc} */ public function serialize() { return serialize($this->items); } /** {@inhertDoc} */ public function unserialize($data) { return $this->items = unserialize($data); } /** {@inhertDoc} **/ public function getIterator() { return new ArrayIterator($this->items); } /** {@inhertDoc} */ public function count($mode = COUNT_NORMAL) { return count($this->items, $mode); } /** * Get a data by key * * @param string $key * * @return mixed */ public function __get($key) { return $this[$key]; } /** * Assigns a value to the specified data * * @param string $key * @param mixed $value * * @return void */ public function __set($key, $value) { $this->items[$key] = $value; } /** * Whether or not an data exists by key * * @param string $key * * @return bool */ public function __isset($key) { return isset($this->items[$key]); } /** * Unsets an data by key * * @param string $key */ public function __unset($key) { unset($this->items[$key]); } /** * Assigns a value to the specified offset * * @param string $offset * @param mixed $value * * @return void */ public function offsetSet($offset, $value) { $this->items[$offset] = $value; } /** * Whether or not an offset exists * * @param string $offset * * @access public * * @return bool */ public function offsetExists($offset) { return isset($this->items[$offset]); } /** * Unsets an offset * * @param string $offset * * @return array */ public function offsetUnset($offset) { if ($this->offsetExists($offset)) { unset($this->items[$offset]); } } /** * Returns the value at specified offset * * @param string $offset * * @return mixed */ public function offsetGet($offset) { return $this->offsetExists($offset) ? array_get($this->items, $offset) : null; } } ``` 然后在我們初始化eloquent的方裝載這個(gè)分頁(yè)類(lèi)到eloquent中就好: ```php //... use ResterPaginator; // 注冊(cè)分頁(yè)類(lèi) Capsule::setPaginator(function() use ($app, $config) { return new Paginator($app->request, $config->get("pager", "page")); }); //... ``` 完整的eloquent初始化步驟請(qǐng)參考: https://github.com/overtrue/rester/blob/master/start/eloquent.php 然后我們就可以正常使用分頁(yè)功能了: ```php $users = User::paginate(15); $users = User::where("status", 1)->paginate(15); ... ``` 因?yàn)樯厦娴姆猪?yè)類(lèi)實(shí)現(xiàn)了常用的[預(yù)定義接口](http://php.net/manual/zh/reserved.interfaces.php), 所以你可以很方便的使用分頁(yè)結(jié)果: ```php // 遍歷 foreach ($users as $user) { // do sth. } // json encode $json = json_encode($users); // count $count = count($users); //... ``` 另外還考慮到了大家不一定全用它寫(xiě)接口用,所以分頁(yè)類(lèi)同樣實(shí)現(xiàn)了Laravel里的生成分頁(yè)鏈接的方法:`$users->links()`, 它會(huì)生成bootstrap格式的分頁(yè)列表: ```html ``` demo: ```php
name; ?>
links(); ?>

OK,那么現(xiàn)在你就可以很方便的在你的項(xiàng)目里無(wú)憂(yōu)的使用Eloquent啦。

  

原文: http://overtrue.me/2014/11/25/using-eloquent-outsite-laravel-2.html

  

上一篇:http://segmentfault.com/blog/overtrue/1190000002468218

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/31907.html

相關(guān)文章

  • laravel5.5手寫(xiě)教程4Eloquent ORM分頁(yè)及軟刪除

    摘要:從而達(dá)到了軟刪除。不過(guò),你可以通過(guò)在查詢(xún)中調(diào)用方法來(lái)強(qiáng)制查詢(xún)已被軟刪除的模型方法也可以被用在關(guān)聯(lián)查詢(xún)只取出軟刪除數(shù)據(jù)會(huì)只取出軟刪除數(shù)據(jù)恢復(fù)被軟刪除的模型有時(shí)候你可能希望取消刪除一個(gè)已被軟刪除的模型。 Laravel 有三寶,路由、容器和 Eloquent ORM,Eloquent ORM。我個(gè)人一直比較推薦于在實(shí)際操作中學(xué)習(xí),之前簡(jiǎn)單了解了路由和Eloquent ORM的基本用法,今天...

    mindwind 評(píng)論0 收藏0
  • 【整理】LaravelEloquent ORM 相關(guān)操作

    摘要:軟刪除當(dāng)模型被軟刪除后,它們并沒(méi)有真的從數(shù)據(jù)庫(kù)刪除,而是在模型上設(shè)置一個(gè)屬性并插入數(shù)據(jù)庫(kù),如果模型有一個(gè)非空值,那么該模型已經(jīng)被軟刪除了。 Laravel 中Eloquent ORM 相關(guān)操作 定義 操作 獲?。ú樵?xún)) 獲取集合,(查詢(xún)列表) 返回值是 IlluminateDatabaseEloquentCollection 的一個(gè)實(shí)例 獲取所有的數(shù)據(jù) use AppUser; $us...

    dongfangyiyu 評(píng)論0 收藏0
  • Lumen 初體驗(yàn)(

    摘要:的現(xiàn)狀目前是版本,是基于開(kāi)發(fā)。入口文件啟動(dòng)文件和配置文件框架的入口文件是。在路由中指定控制器類(lèi)必須寫(xiě)全命名空間,不然會(huì)提示找不到類(lèi)。目前支持四種數(shù)據(jù)庫(kù)系統(tǒng)以及。使用時(shí)發(fā)生錯(cuò)誤,因?yàn)樵谖募?,的默認(rèn)驅(qū)動(dòng)是。 最近使用 Lumen 做了 2 個(gè)業(yè)余項(xiàng)目,特此記錄和分享一下。 Lumen 的介紹 在使用一項(xiàng)新的技術(shù)時(shí),了解其應(yīng)用場(chǎng)景是首要的事情。 Lumen 的口號(hào):為速度而生的 La...

    Cheriselalala 評(píng)論0 收藏0
  • laravel手動(dòng)創(chuàng)建數(shù)組分頁(yè)

    摘要:目前,無(wú)法高效執(zhí)行使用語(yǔ)句的分頁(yè)操作。如果你需要在分頁(yè)結(jié)果集中使用,建議你查詢(xún)數(shù)據(jù)庫(kù)并手動(dòng)創(chuàng)建分頁(yè)器。手動(dòng)創(chuàng)建分頁(yè)如果你想手動(dòng)創(chuàng)建分頁(yè)實(shí)例并且最終得到一個(gè)數(shù)組類(lèi)型的結(jié)果,可以根據(jù)需求來(lái)創(chuàng)建或者實(shí)例來(lái)實(shí)現(xiàn)。 showImg(https://segmentfault.com/img/bVbbGos?w=640&h=400); laravel分頁(yè)功能: 有幾種方法可以對(duì)數(shù)據(jù)進(jìn)行分頁(yè)。最簡(jiǎn)單的...

    acrazing 評(píng)論0 收藏0
  • Laravel 5.7 正式發(fā)布,同時(shí)啟動(dòng)中文翻譯

    摘要:版本現(xiàn)在正式發(fā)布了,每個(gè)人都可以使用。該版本引入了一些新特性并修復(fù)了很多,改進(jìn)超過(guò)了版本。我們正在翻譯中文文檔,這是個(gè)系統(tǒng)性學(xué)習(xí)的好機(jī)會(huì),感興趣的同學(xué)請(qǐng)前往 showImg(https://segmentfault.com/img/remote/1460000016281269); 「Laravel 5.7?」版本現(xiàn)在正式發(fā)布了,每個(gè)人都可以使用。該版本引入了一些新特性并修復(fù)了很多 b...

    Yujiaao 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<