laravel-medailibrary getFirstMediaUrl“ images”对于连接的表不起作用

问题描述

我在从laravel-medailibrary中的联接表中获取媒体时遇到问题,我使用getFirstMediaUrl(“ images”)从一个表中获取照片,并且可以工作,但是如果我加入两个或三个表,则无法工作我解决了吗?

我想从用户分享的帖子中获取照片。

此帖子表:

enter image description here

这是share_tb表:

enter image description here

这是用户表:

enter image description here

这是媒体表:

enter image description here

解决方法

尝试一些方法后,我找到了答案:

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);

}

通过此资源集合,我得到了徽标:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...