laravellaravel-medialibrary

laravel-medailibrary getFirstMediaUrl("images") not work for joined tables


I have a problem getting media from a joined table in laravel-medailibrary, I used getFirstMediaUrl("images") to get photos from one table and it works, but if I join two or three tables it not work, how can I solve it?

I want to get photos from those posts that shared by a user.

this post table: enter image description here

this is share_tb table:

enter image description here

this is users table:

enter image description here

this is the media table:

enter image description here


Solution

  • I find my answer after trying some ways:

    public function getPosts(Request $request)
    {
    
    
        $result = [];
    
        $postID = DB::table("share_tb")->where("user_id", Auth::user()->id)->get();
    
        foreach ($postID as $id) {
    
            if (count(Post::where("id", $id->related_id)->get()) > 0) {
                $posts = Post::where("id", $id->related_id)->get();
                foreach ($posts as $post) {
    
                    // $result = $post->getMedia('images');
                    array_push($result, [
                        "comment_count" => getTotalComment($post->id),
                        "course_id" => $post->course_id,
                        "id" => $post->id,
                        'post_image' => count($post->getMedia('images')) > 0 ? $post->getMedia('images')[0]->getFullUrl('big') : "",
                        'logo'=>GetCourseLogo::collection(Course::where('course_id',$post->course_id)->get()),
                        "post_author" => $post->post_author,
                        "post_date" => $post->post_date,
                        "post_excerpt" => $post->post_excerpt,
                        "post_modified" => $post->post_modified,
                        "post_parent" => $post->post_parent,
                        "post_title" => $post->post_title,
                        "post_type" => $post->post_type,
                    ]);
                }
            }
    
        }
        return Response()->json($result);
    
    }
    

    and by this resource collection, I get the logo:

    class GetCourseLogo extends JsonResource
      {
         public function toArray($request)
           {
              return $this->getFirstMediaUrl('logo');
           }
      }