inrsaurabh
2 months ago
300
10
Laravel

How to get related data of a collection (lumen) ?

Posted 2 months ago by inrsaurabh

In User model

public function folders()
   {
       return $this->belongsToMany(Folder::class)->withTimestamps();
   }

In Folder model

public function medias()
   {
       return $this->belongsToMany(Media::class)->withTimestamps();
   }

In Media model

 public function folders()
    {
        return $this->belongsToMany(Folder::class)->withTimestamps();
    }

I am trying to get all the media related to a user.

As Media is not related to user directly i am trying as

  $userData = User::find($user_id);
  $relatedFolder = $userData->folders()->get(['folder_id'])->map(function($media) {
          return $media->folder_id;
})->toArray();
return $folder = Folder::whereIn('id',  $relatedFolder)->medias()->get();

I am getting Call to undefined method Illuminate\\Database\\Eloquent\\Builder::medias()"

So how to get related data on Illuminate\Database\Eloquent\Collection

Please sign in or create an account to participate in this conversation.