Laravel API控制器返回对象以及其他参数



在API控制器索引函数$response中保存来自db表的media项目的id's

if ($is_published_pack == 'true') {
return new CourseResource(Course::with(['assigned_content_packs'])
->whereIn('id', function($query) use($content_pack_id){
$query->select('course_id')
->from('content_pack_course')
->where('content_pack_id', $content_pack_id);
})
->get());
}

如果$is_published_pack == 'false'

我成功返回

return new CourseResource($response);

当满足$is_published_packtrue条件时,如何在里面追加$response

$response值为

$response=Course::all()->toArray();

例如,我尝试过这样做,但它会抛出错误

return new CourseResource($response)(Course::with(['assigned_content_packs'])->whereIn('id', function($query) use($content_pack_id){
$query->select('course_id')
->from('content_pack_course')
->where('content_pack_id', $content_pack_id);
})
->get());

完整的API控制器索引功能:

public function index()
{
abort_if(Gate::denies('course_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$response=Course::all()->toArray();
$allData = [];
foreach (Course::all() as $ids=>$CMF) {
UNSET($response[$ids]['media']);
$data_sequence = DB::table('media_sequence')->where('data_id', $CMF["id"])->where('type','CMF')->first();
$data_id=$data_sequence->id;
$data_sequence = json_decode($data_sequence->data_sequence);
$data = [];
$data["id"] = $CMF["id"];
$data["title"] = $CMF["title"];
$response[$ids]['category_title']=$CMF->category->title;

foreach ($data_sequence as $id => $dataSeq) {
if ($dataSeq->type == "Text") {
$response[$ids]['media'][]=["id"=>$data_id,"text"=> $dataSeq->name,"mime_type"=>"text"];
} elseif ($dataSeq->type == "file") {
foreach ($CMF["media"] as $file) {
if (str::slug($dataSeq->name) == str::slug($file["file_name"])) {
$file["thumb"] = $file->getUrl('video_thumb');
$response[$ids]['media'][]=$file;
$response[$ids]['category']=$CMF->category;
$response[$ids]['assigned_teams']=$CMF->assigned_teams;
$response[$ids]['pain_tags']=$CMF->pain_tags;   
}
}
}
}
$allData[] = $data;
}

$user_role_id = auth()->user()->role_id;

if($user_role_id == 3) {
$user_id = auth()->user()->id;
$email = auth()->user()->email;

$company = DB::table('companies')->where('username' , $email)->first();
$company_email = $company->username;

$company_id = $company->id; 
$company_content_pack = DB::table('company_content_pack')->where('company_id' , $company_id)->first();
$content_pack_id = $company_content_pack->content_pack_id;

$content_packs = DB::table('content_packs')->where('id' , $content_pack_id)->first();
$is_published_pack = $content_packs->is_published;

if ($is_published_pack == 'true') {

return new CourseResource(Course::with(['assigned_content_packs'])->whereIn('id', function($query) use($content_pack_id){
$query->select('course_id')
->from('content_pack_course')
->where('content_pack_id', $content_pack_id);
})
->get());
} 
}
return new CourseResource($response);
}

你可以试试这个:

if ($is_published_pack == 'true') {
$target_courses = Course::with(['assigned_content_packs'])
->whereIn('id', function($query) use($content_pack_id){
$query->select('course_id')
->from('content_pack_course')
->where('content_pack_id', $content_pack_id);
})
->get()
->toArray();
$tmp_resp = [];
foreach($target_courses as $record) {
$record['media'] = collect($target_courses)
->where('id',$record['id'])->first()['media'];
array_push($tmp_resp, $record);
}
return new CourseResource($tmp_resp);
} 

希望这能解决你的问题。

相关内容

  • 没有找到相关文章

最新更新