Skip to content
Advertisement

Laravel API controller return object along with other arguments

In API controller index function $response holds id's of media items from db table

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

And if $is_published_pack == 'false'

I’m successfully returning

return new CourseResource($response);

How to append $response inside when $is_published_pack true condition is met?

$response value is

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

I’ve tried for example doing this but it throws error

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

Complete API controller index function:

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

Advertisement

Answer

you may try this:

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

hope this solves your issue.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement