umefarooq's avatar

How to paginate multiple morphed relations.

I am sharing folders and images with many to many morph. i am saving folder and images details in folder and images table. while sharing them saving basic info in share folder and folder and images id in morphed shareable table. i want to paginate both relations in 1 query here are my models

Images Model

class Image extends Model
{
    
    public function folder()
    {
        return $this->belongsTo('App\Folder');
    }

    public function share()
    {
        return $this->morphToMany('App\Share', 'shareable');
    }
}

Folder model

class Folder extends Model
{
    
    public function share()
    {
        return $this->morphToMany('App\Share', 'shareable');
    }
}

Share model

class Share extends Model
{
    
    public function assets()
    {
        return $this->morphedByMany('App\Image', 'shareable');
    }

    public function folders()
    {
        return $this->morphedByMany('App\Folder', 'shareable');
    }
}

following code will work with pagination or not.

$share = Share::first(1);

$assets = $share->with('folder','image')->paginate();

what i am thinking to have one method in share model with hasMany with shareable to get folder and images with pagination. Or there is better way to get it.

0 likes
0 replies

Please or to participate in this conversation.