laravel-medailibrary getFirstMediaUrl("images") 不适用于连接表
laravel-medailibrary getFirstMediaUrl("images") not work for joined tables
我在从 laravel-medailibrary 中加入的 table 获取媒体时遇到问题,我使用 getFirstMediaUrl("images") 从一个 table 获取照片并且它有效,但是如果我加入两个或三个 table 就不行了,我该如何解决?
我想从用户分享的 post 中获取照片。
这个posttable:
这是share_tb table:
这是用户 table:
这是媒体 table:
我尝试了一些方法后找到了答案:
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');
}
}
我在从 laravel-medailibrary 中加入的 table 获取媒体时遇到问题,我使用 getFirstMediaUrl("images") 从一个 table 获取照片并且它有效,但是如果我加入两个或三个 table 就不行了,我该如何解决?
我想从用户分享的 post 中获取照片。
这个posttable:
这是share_tb table:
这是用户 table:
这是媒体 table:
我尝试了一些方法后找到了答案:
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');
}
}