1

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);
}
7
  • I didn't understand completely. You want to append the $response=Course::all()->toArray(); to the if body resource when it met, right? Commented May 19, 2022 at 11:56
  • 1
    not getting your question properly @Jessica Kimble Commented May 19, 2022 at 12:03
  • Yes @A.Seddighi I need to append $response when true is met Commented May 19, 2022 at 12:43
  • Why do you want to do that? you can simply remove the where() from your query and all rows returned Commented May 19, 2022 at 12:47
  • Yes I know, I return all rows when false is met properly The problem is appending $response when true is met Commented May 19, 2022 at 12:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This gave me api response of both arrays after each other, but not merged I think I need to apply a array_filter on $response that it will produce output only with(['assigned_content_packs'])
@JessicaKimble, I posted the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.