Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginate on hasManyThrough returns first level relation #4171

Closed
andrewdacenko opened this issue Apr 17, 2014 · 5 comments
Closed

Paginate on hasManyThrough returns first level relation #4171

andrewdacenko opened this issue Apr 17, 2014 · 5 comments

Comments

@andrewdacenko
Copy link

For example:

Database Structure

countries
  id
  name

users
  id
  email
  password
  country_id

posts
  id
  title
  description
  user_id

And in Country model we have:

<?php
class Country extends Eloquent {
    public function users()
    {
        return $this->hasMany('User');
    }

    public function posts()
    {
        return $this->hasManyThrough('Post', 'User');
    }
}
<?php
class User extends Eloquent {
    public function country()
    {
        return $this->belongsTo('Country');
    }

    public function posts()
    {
        return $this->hasMany('Post');
    }
}

If we search for all post in country like this $country->posts - we get the correct data, also using $country->posts()->take(10)->get() - we also get right data from posts table.

But if we call for $country->posts()->paginate() - we get paginated data from users table.

@taylorotwell
Copy link
Member

You mean you "only" get paginated data from the users table, or the paginated data includes both users and posts?

@andrewdacenko
Copy link
Author

Yeap, paginate returns Only users data, no posts data included.

@taylorotwell
Copy link
Member

This has been fixed.

@i-coder
Copy link

i-coder commented Mar 10, 2022

no not fixed, connection is lost when adding pagination

@i-coder
Copy link

i-coder commented Mar 10, 2022

class Product extends Model
{ 
  public function files()
    {
        return $this->belongsToMany(Files::class, 'entity_files', 'entity_id', 'file_id')->select(['files.path', 'entity_files.file_id']);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(Category::class, '1c_cat_id', '1c_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function translations()
    {
        return $this->belongsTo(Translations::class, 'id', 'product_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function prices()
    {
        return $this->belongsTo(Prices::class, 'id', 'product_id');
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function counts()
    {
        return $this->belongsTo(Remains::class, 'id', 'product_id');
    }
    
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     */
    public function prisons()
    {

        return $this->hasManyThrough(
            Prison::class,
            Prices::class,
            null, 'city_id', null
        );
    }
}
public function getAllProductTable($request)
    {
        $result = $this->startConditions()
            ->where('products.is_active', 1)
            ->with(['prisons', 'files', 'translations',
                'prices' => fn($query) => $query->where('city_id', '=', $request->prisons_id),
                'counts' => fn($query) => $query->where('controlled_count', '=', '1')->where('city_id', '=', $request->prisons_id)])
            ->orderBy('products.id', 'DESC');

        return $result->paginate($request->perPage);
    }

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants