z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.14k stars 2.81k forks source link

自定义表格数据源,重写了model的paginate()方法之后,还是展示不出来自定义的数据 #3370

Closed hxhac closed 5 years ago

hxhac commented 5 years ago

Description:

参考文档这块表格数据源,代码直接粘到项目里,但是grid里的模型,没走这个paginate()方法

    public function paginate()
    {
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);
        $start = ($page-1)*$perPage;

        $data = file_get_contents("https://api.douban.com/v2/movie/in_theaters?city=上海&start=$start&count=$perPage");
        $data = json_decode($data, true);
        $movies = static::hydrate($data['subjects']);
        debug($movies);

        $paginator = new LengthAwarePaginator($movies, $data['total'], $perPage);
        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
    protected function grid()
    {
        $statsDaily = new StatsDaily();
        dd($statsDaily->paginate());
        $grid = new Grid(new StatsDaily(), function (Grid $grid) {
            dd($grid->all());
        ....
    }

必须要指定走paginate()方法,才能打印出来对应的数据;直接new StatsDaily,paginate()不生效;

Steps To Reproduce:

LengthAwarePaginator {#2055 ▼
  #total: 1
  #lastPage: 1
  #items: Collection {#2065 ▼
    #items: array:1 [▼
      0 => StatsDaily {#2062 ▼
        #table: "stats_daily"
        #userMity: UserMity {#2063 ▼
          #table: "user_mity"
          #fillable: array:5 [▼
            0 => "msg"
            1 => "mobile"
            2 => "device_code"
            3 => "date"
            4 => "is_app"
          ]
          #connection: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: false
          +wasRecentlyCreated: false
          #attributes: []
          #original: []
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▼
            0 => "*"
          ]
        }
        #productTrack: ProductTrack {#2064 ▼
          #table: "product_track"
          #fillable: array:6 [▼
            0 => "app_id"
            1 => "user_id"
            2 => "product_id"
            3 => "market"
            4 => "click_origin"
            5 => "date"
          ]
          #connection: null
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: false
          +wasRecentlyCreated: false
          #attributes: []
          #original: []
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▼
            0 => "*"
          ]
        }
        #connection: "mysql"
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:27 [▼
          "id" => 1
          "app_id" => 1
          "channel_id" => 1
          "nickname" => "yyz"
          "date_t" => "2019-05-17"
          "num_reg" => 500
          "num_nreg" => 490
          "num_login" => 400
          "num_android_login" => 100
          "num_ios_login" => 200
          "num_nlogin" => 200
          "num_android_nlogin" => 100000
          "num_ios_nlogin" => 10000
          "rate_login_reg" => "0.68"
          "rate_nlogin_nreg" => "0.58"
          "num_apply" => 852
          "num_napply" => 78
          "times_apply" => 587
          "times_napply" => 454
          "rate_times_num" => 454
          "rate_ntimes_num" => 52
          "download_android_times" => 785
          "download_ios_times" => 4524
          "num_member_keep" => 54
          "num_times_keep" => 452
          "created_at" => "2019-05-17 10:50:33"
          "updated_at" => "2019-05-17 10:50:36"
        ]
        #original: array:27 [▼
          "id" => 1
          "app_id" => 1
          "channel_id" => 1
          "nickname" => "yyz"
          "date_t" => "2019-05-17"
          "num_reg" => 500
          "num_nreg" => 490
          "num_login" => 400
          "num_android_login" => 100
          "num_ios_login" => 200
          "num_nlogin" => 200
          "num_android_nlogin" => 100000
          "num_ios_nlogin" => 10000
          "rate_login_reg" => "0.68"
          "rate_nlogin_nreg" => "0.58"
          "num_apply" => 852
          "num_napply" => 78
          "times_apply" => 587
          "times_napply" => 454
          "rate_times_num" => 454
          "rate_ntimes_num" => 52
          "download_android_times" => 785
          "download_ios_times" => 4524
          "num_member_keep" => 54
          "num_times_keep" => 452
          "created_at" => "2019-05-17 10:50:33"
          "updated_at" => "2019-05-17 10:50:36"
        ]
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: []
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
        #guarded: array:1 [▼
          0 => "*"
        ]
      }
    ]
  }
  #perPage: 1
  #currentPage: 1
  #path: "http://gs.loanbank.com/admin/stats_daily"
  #query: []
  #fragment: null
  #pageName: "page"
}

这个是我自定义的数据源,用

        $statsDaily = new StatsDaily();
        dd($statsDaily->paginate());

打印出来的;

直接new StastDaily拿到的数据,并没有走paginate()方法

hxhac commented 5 years ago

有一个不是很懂的点,这个重写的paginate()方法是Builder下的那个paginate()吗,那个paginate()应该是一个静态方法,带了一堆参数吧;我怀疑是我重写错方法了;

454335025 commented 5 years ago

$grid->disablePagination() 看看是不是禁用了

hxhac commented 5 years ago

$grid->disablePagination() 看看是不是禁用了

查了一下,没有禁用这个;所以很头疼,刚刚一直在翻Grid这块的源码,感觉不太应该啊; @454335025

z-song commented 5 years ago

必须要指定走paginate()方法,才能打印出来对应的数据;直接new StatsDaily,paginate()不生效;

这不是正常现象吗?

hxhac commented 5 years ago

那调用的那边应该怎么写呢? @z-song

newbrash commented 5 years ago

$grid->disablePagination() 看看是不是禁用了

查了一下,没有禁用这个;所以很头疼,刚刚一直在翻Grid这块的源码,感觉不太应该啊; @454335025

我都xdebug了两天了,如果是index确实是走paginate,但是filter筛选控件不走那里啊

454335025 commented 5 years ago

https://github.com/z-song/laravel-admin/commit/1fe75fcfe20ead25438ea3a75c22d222bee5849c 看这个,我刚提交修复的

newbrash commented 5 years ago

可以了,多谢,一直很想知道源码怎么看,但是一碰到_call方法就晕,除非是写的很直白的代码能看懂,但是框架的源码都是封装了好几层的,就是这几层带着些什么知识去阅读比较好,有什么技巧吗

------------------ 原始邮件 ------------------ 发件人: "NDDDA"notifications@github.com; 发送时间: 2019年5月20日(星期一) 晚上8:02 收件人: "z-song/laravel-admin"laravel-admin@noreply.github.com; 抄送: "布鲁布鲁"2199374997@qq.com;"Comment"comment@noreply.github.com; 主题: Re: [z-song/laravel-admin] 自定义表格数据源,重写了model的paginate()方法之后,还是展示不出来自定义的数据 (#3370)

1fe75fc 看这个,我刚提交修复的

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.