How to paginate a "has many" relationship that is ordered?

Saulo Silva

I would like to paginate the following relationship (a Category having many Apps):

class Category extends Model
{
    public function apps()
    {
        return $this->hasMany('App\App')->orderBy('current_price', 'asc');
    }
}

The problem is, when I add ->paginate(10); to the end of that line, I get the following error:

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

What am I missing here?

Thomas Kim

Have you tried this?

$category = Category::first();
$apps = $category->apps()->paginate(10);
return view('example', compact('category', 'apps'));

Then, on your view, you can just loop through the apps.

@foreach ($apps as $app)
    {{ $app->id }}
@endforeach

{!! $apps->render() !!}

If you want to set it as a relationship to category, you can use the setRelation method:

$category = Category::first();
$category->setRelation('apps', $category->apps()->paginate(10));
return view('example', compact('category');

Then in your view:

@foreach ($category->apps as $app)
    {{ $app->id }}
@endforeach

{!! $category->apps->render() !!}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails includes ordered by first associate of a has_many relationship

From Dev

How do I have multiple has_many to has_many relationship between two models?

From Dev

Ember.js: How to represent a has-many model relationship?

From Dev

How can I add default values for ember has many relationship?

From Dev

How to accumulate results from has_many relationship?

From Dev

How to handle belongs to and has many relationship in Backbone js

From Dev

ruby on rails how to get latest records from has many relationship

From Dev

How to create join table record in Rails, has many: through relationship

From Dev

How does a has_many relationship work with new?

From Dev

How to accumulate results from has_many relationship?

From Dev

How can I show attribute in a has many relationship? - RoR

From Dev

How to paginate objects from a relationship

From Dev

How to paginate query results ordered in descending order

From Dev

How to paginate query results ordered in descending order

From Dev

Rails active record query: rendering only the most recent child object (ordered by child created_at date) in has_many relationship

From Dev

Has many through relationship in rails

From Dev

Seeding with a has_many relationship

From Dev

Exclusive join on has many relationship

From Dev

MongoDB / Mongoose has many Relationship

From Dev

Rails has many through relationship

From Dev

how to model a many-to-many relationship, associated with a has-many relationship rails

From Dev

Laravel 4.1: How to paginate eloquent eager relationship?

From Dev

Laravel 4.1: How to paginate eloquent eager relationship?

From Dev

How do I express a "has many through" relationship in Entity Framework 5?

From Dev

How to build has_many :through relationship between the same Model (User) in Rails?

From Dev

With Elixir Ecto, how do I add a has_many relationship in a migration?

From Dev

ActiveRecord how to add existing record to association in has_many :through relationship in rails?

From Dev

How do I submit a collection_check_box through the controller with a has_many through relationship?

From Dev

How do I create an object of a specified type through a has_many relationship?

Related Related

  1. 1

    Rails includes ordered by first associate of a has_many relationship

  2. 2

    How do I have multiple has_many to has_many relationship between two models?

  3. 3

    Ember.js: How to represent a has-many model relationship?

  4. 4

    How can I add default values for ember has many relationship?

  5. 5

    How to accumulate results from has_many relationship?

  6. 6

    How to handle belongs to and has many relationship in Backbone js

  7. 7

    ruby on rails how to get latest records from has many relationship

  8. 8

    How to create join table record in Rails, has many: through relationship

  9. 9

    How does a has_many relationship work with new?

  10. 10

    How to accumulate results from has_many relationship?

  11. 11

    How can I show attribute in a has many relationship? - RoR

  12. 12

    How to paginate objects from a relationship

  13. 13

    How to paginate query results ordered in descending order

  14. 14

    How to paginate query results ordered in descending order

  15. 15

    Rails active record query: rendering only the most recent child object (ordered by child created_at date) in has_many relationship

  16. 16

    Has many through relationship in rails

  17. 17

    Seeding with a has_many relationship

  18. 18

    Exclusive join on has many relationship

  19. 19

    MongoDB / Mongoose has many Relationship

  20. 20

    Rails has many through relationship

  21. 21

    how to model a many-to-many relationship, associated with a has-many relationship rails

  22. 22

    Laravel 4.1: How to paginate eloquent eager relationship?

  23. 23

    Laravel 4.1: How to paginate eloquent eager relationship?

  24. 24

    How do I express a "has many through" relationship in Entity Framework 5?

  25. 25

    How to build has_many :through relationship between the same Model (User) in Rails?

  26. 26

    With Elixir Ecto, how do I add a has_many relationship in a migration?

  27. 27

    ActiveRecord how to add existing record to association in has_many :through relationship in rails?

  28. 28

    How do I submit a collection_check_box through the controller with a has_many through relationship?

  29. 29

    How do I create an object of a specified type through a has_many relationship?

HotTag

Archive