【问题标题】:Laralvel 5.2 Pagination does not returning from and to paramsLaravel 5.2 Paginate 不会从参数返回和返回参数
【发布时间】:2016-11-21 04:17:19
【问题描述】:

我正在做这样的事情

$users = CompanyModel::paginate(5);
print_r($users);exit;

当我打印这个时,它会打印“Illuminate\Pagination\LengthAwarePaginator Object”,它会在其中返回总、每页、当前页等参数。

现在我需要像 from 和 to 这样的参数,我将在前端用于分页。

请帮忙!!

我的回应:

 Illuminate\Pagination\LengthAwarePaginator Object
 (
[total:protected] => 21
[lastPage:protected] => 5
[items:protected] => Illuminate\Database\Eloquent\Collection Object
    (
        [items:protected] => Array
            (
                [0] => DayJibe\DAL\Company\Models\CompanyModel Object
                    (
                        [table:protected] => companies
                        [fillable:protected] => Array
                            (
                                [0] => company_name
                                [1] => user_first_name
                                [2] => user_email
                                [3] => plan_name
                                [4] => total_users
                                [5] => monthly_payment_amount
                                [6] => term_ending
                                [7] => sign_up_date
                                [8] => receive_special_offers_emails
                                [9] => time_spent_logged_in
                                [10] => storage_used
                            )

                        [connection:protected] => 
                        [primaryKey:protected] => id
                        [keyType:protected] => int
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [id] => 1
                                [company_name] => JohnSmithConsulting
                                [user_first_name] => John
                                [user_email] => admin@gmail.com
                                [plan_name] => Basic
                                [total_users] => 4
                                [monthly_payment_amount] => 10
                                [term_ending] => 2016-01-02 00:00:00
                                [sign_up_date] => 2015-01-02 00:00:00
                                [receive_special_offers_emails] => Y
                                [time_spent_logged_in] => 15
                                [storage_used] => 100
                                [created_at] => 2016-07-14 11:37:51
                                [updated_at] => 2016-07-14 11:37:51
                            )

                        [original:protected] => Array
                            (
                                [id] => 1
                                [company_name] => JohnSmithConsulting
                                [user_first_name] => John
                                [user_email] => admin@gmail.com
                                [plan_name] => Basic
                                [total_users] => 4
                                [monthly_payment_amount] => 10
                                [term_ending] => 2016-01-02 00:00:00
                                [sign_up_date] => 2015-01-02 00:00:00
                                [receive_special_offers_emails] => Y
                                [time_spent_logged_in] => 15
                                [storage_used] => 100
                                [created_at] => 2016-07-14 11:37:51
                                [updated_at] => 2016-07-14 11:37:51
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [dateFormat:protected] => 
                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                        [wasRecentlyCreated] => 
                    )

                [1] => DayJibe\DAL\Company\Models\CompanyModel Object
                    (
                        [table:protected] => companies
                        [fillable:protected] => Array
                            (
                                [0] => company_name
                                [1] => user_first_name
                                [2] => user_email
                                [3] => plan_name
                                [4] => total_users
                                [5] => monthly_payment_amount
                                [6] => term_ending
                                [7] => sign_up_date
                                [8] => receive_special_offers_emails
                                [9] => time_spent_logged_in
                                [10] => storage_used
                            )

                        [connection:protected] => 
                        [primaryKey:protected] => id
                        [keyType:protected] => int
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [id] => 2
                                [company_name] => James Engineering
                                [user_first_name] => James
                                [user_email] => James@gmail.com
                                [plan_name] => Enterprise
                                [total_users] => 50
                                [monthly_payment_amount] => 250
                                [term_ending] => 2016-04-05 00:00:00
                                [sign_up_date] => 2016-01-02 00:00:00
                                [receive_special_offers_emails] => N
                                [time_spent_logged_in] => 25
                                [storage_used] => 250
                                [created_at] => 2016-07-15 06:33:34
                                [updated_at] => 2016-07-15 06:33:34
                            )

                        [original:protected] => Array
                            (
                                [id] => 2
                                [company_name] => James Engineering
                                [user_first_name] => James
                                [user_email] => James@gmail.com
                                [plan_name] => Enterprise
                                [total_users] => 50
                                [monthly_payment_amount] => 250
                                [term_ending] => 2016-04-05 00:00:00
                                [sign_up_date] => 2016-01-02 00:00:00
                                [receive_special_offers_emails] => N
                                [time_spent_logged_in] => 25
                                [storage_used] => 250
                                [created_at] => 2016-07-15 06:33:34
                                [updated_at] => 2016-07-15 06:33:34
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [dateFormat:protected] => 
                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                        [wasRecentlyCreated] => 
                    )

                [2] => DayJibe\DAL\Company\Models\CompanyModel Object
                    (
                        [table:protected] => companies
                        [fillable:protected] => Array
                            (
                                [0] => company_name
                                [1] => user_first_name
                                [2] => user_email
                                [3] => plan_name
                                [4] => total_users
                                [5] => monthly_payment_amount
                                [6] => term_ending
                                [7] => sign_up_date
                                [8] => receive_special_offers_emails
                                [9] => time_spent_logged_in
                                [10] => storage_used
                            )

                        [connection:protected] => 
                        [primaryKey:protected] => id
                        [keyType:protected] => int
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [id] => 3
                                [company_name] => Joe Engineering LTD.
                                [user_first_name] => Joe
                                [user_email] => Joe@gmail.com
                                [plan_name] => Freelance
                                [total_users] => 36
                                [monthly_payment_amount] => 500
                                [term_ending] => 2016-08-08 00:00:00
                                [sign_up_date] => 2015-08-08 00:00:00
                                [receive_special_offers_emails] => Y
                                [time_spent_logged_in] => 50
                                [storage_used] => 500
                                [created_at] => 2016-07-15 06:35:39
                                [updated_at] => 2016-07-15 06:35:39
                            )

                        [original:protected] => Array
                            (
                                [id] => 3
                                [company_name] => Joe Engineering LTD.
                                [user_first_name] => Joe
                                [user_email] => Joe@gmail.com
                                [plan_name] => Freelance
                                [total_users] => 36
                                [monthly_payment_amount] => 500
                                [term_ending] => 2016-08-08 00:00:00
                                [sign_up_date] => 2015-08-08 00:00:00
                                [receive_special_offers_emails] => Y
                                [time_spent_logged_in] => 50
                                [storage_used] => 500
                                [created_at] => 2016-07-15 06:35:39
                                [updated_at] => 2016-07-15 06:35:39
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [dateFormat:protected] => 
                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                        [wasRecentlyCreated] => 
                    )

                [3] => DayJibe\DAL\Company\Models\CompanyModel Object
                    (
                        [table:protected] => companies
                        [fillable:protected] => Array
                            (
                                [0] => company_name
                                [1] => user_first_name
                                [2] => user_email
                                [3] => plan_name
                                [4] => total_users
                                [5] => monthly_payment_amount
                                [6] => term_ending
                                [7] => sign_up_date
                                [8] => receive_special_offers_emails
                                [9] => time_spent_logged_in
                                [10] => storage_used
                            )

                        [connection:protected] => 
                        [primaryKey:protected] => id
                        [keyType:protected] => int
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [id] => 4
                                [company_name] => Michal Engineering LTD.
                                [user_first_name] => Michal
                                [user_email] => Michal@gmail.com
                                [plan_name] => Basic
                                [total_users] => 45
                                [monthly_payment_amount] => 400
                                [term_ending] => 2016-09-10 00:00:00
                                [sign_up_date] => 2016-01-05 00:00:00
                                [receive_special_offers_emails] => N
                                [time_spent_logged_in] => 40
                                [storage_used] => 150
                                [created_at] => 2016-07-15 06:36:57
                                [updated_at] => 2016-07-15 06:36:57
                            )

                        [original:protected] => Array
                            (
                                [id] => 4
                                [company_name] => Michal Engineering LTD.
                                [user_first_name] => Michal
                                [user_email] => Michal@gmail.com
                                [plan_name] => Basic
                                [total_users] => 45
                                [monthly_payment_amount] => 400
                                [term_ending] => 2016-09-10 00:00:00
                                [sign_up_date] => 2016-01-05 00:00:00
                                [receive_special_offers_emails] => N
                                [time_spent_logged_in] => 40
                                [storage_used] => 150
                                [created_at] => 2016-07-15 06:36:57
                                [updated_at] => 2016-07-15 06:36:57
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [dateFormat:protected] => 
                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                        [wasRecentlyCreated] => 
                    )

                [4] => DayJibe\DAL\Company\Models\CompanyModel Object
                    (
                        [table:protected] => companies
                        [fillable:protected] => Array
                            (
                                [0] => company_name
                                [1] => user_first_name
                                [2] => user_email
                                [3] => plan_name
                                [4] => total_users
                                [5] => monthly_payment_amount
                                [6] => term_ending
                                [7] => sign_up_date
                                [8] => receive_special_offers_emails
                                [9] => time_spent_logged_in
                                [10] => storage_used
                            )

                        [connection:protected] => 
                        [primaryKey:protected] => id
                        [keyType:protected] => int
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [id] => 5
                                [company_name] => Michal Engineering LTD.
                                [user_first_name] => Michal
                                [user_email] => Michal@gmail.com
                                [plan_name] => Basic
                                [total_users] => 45
                                [monthly_payment_amount] => 400
                                [term_ending] => 0000-00-00 00:00:00
                                [sign_up_date] => 0000-00-00 00:00:00
                                [receive_special_offers_emails] => N
                                [time_spent_logged_in] => 40
                                [storage_used] => 150
                                [created_at] => 2016-07-15 07:58:36
                                [updated_at] => 2016-07-15 07:58:36
                            )

                        [original:protected] => Array
                            (
                                [id] => 5
                                [company_name] => Michal Engineering LTD.
                                [user_first_name] => Michal
                                [user_email] => Michal@gmail.com
                                [plan_name] => Basic
                                [total_users] => 45
                                [monthly_payment_amount] => 400
                                [term_ending] => 0000-00-00 00:00:00
                                [sign_up_date] => 0000-00-00 00:00:00
                                [receive_special_offers_emails] => N
                                [time_spent_logged_in] => 40
                                [storage_used] => 150
                                [created_at] => 2016-07-15 07:58:36
                                [updated_at] => 2016-07-15 07:58:36
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [dateFormat:protected] => 
                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                        [wasRecentlyCreated] => 
                    )

            )

    )

[perPage:protected] => 5
[currentPage:protected] => 1
[path:protected] => http://dayjibe.localhost.com/api/v1.0/company
[query:protected] => Array
    (
    )

[fragment:protected] => 
[pageName:protected] => page

)

【问题讨论】:

  • 请发布你得到了什么print_r($users);
  • 听起来像是阅读手册就能回答的问题
  • 请快速查看文档here。在大多数情况下,它会立即提供帮助。
  • 我检查了几次文件 :)

标签: php laravel-5.2


【解决方案1】:

我发现了这个: $users = CompanyModel::paginate(5); 响应::json($users);

如果我们这样做,那么在 JSON 响应中它会自动添加 per_page、current_page、from、to 等参数。

所以是的,我们需要做 Response::json 然后从中获取所需的 Paramus。

谢谢大家:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 2021-11-15
    • 2016-03-28
    相关资源
    最近更新 更多