laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.39k stars 10.98k forks source link

Paginate on hasManyThrough returns first level relation #4171

Closed andrewdacenko closed 10 years ago

andrewdacenko commented 10 years ago

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 commented 10 years ago

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

andrewdacenko commented 10 years ago

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

taylorotwell commented 10 years ago

This has been fixed.

i-coder commented 2 years ago

no not fixed, connection is lost when adding pagination

i-coder commented 2 years ago
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