get_field not works at foreach loop in php

I am a beginner at php, i’m trying to get Gallery Images of every post in the loop to return it in Rest API

    $mylink = $wpdb->get_results($query);
    if (count($mylink) > 0){
        foreach ($mylink as $prj){
            $galleries = Array();
            $pid = $prj->id;
            $gs = get_field('gallery', $pid);
            if($gs == null){
                $gs = Array();
            }

            foreach ($gs as $g){
                $c = new stdclass();
                $c -> id = $g["ID"];
                $c -> url = $g["url"];
                $galleries [] = $c;
            }
            $prj -> PID = $pid;
            $prj -> GS = $gs;
            $prj -> gallery  = $galleries;
            $prj -> image = ''.wp_get_attachment_url( get_post_thumbnail_id($prj->id) );
        }
        $results_array['msg'] = 'Success';
        $results_array['status'] = true;
        $results_array['result'] = $mylink;
        $results_array['current_time'] = date("Y-m-d H:i:s");
        return $results_array;
    }

I can get the Gallery Images of every single post in page but it doesn’t show any thing in the Rest API

Related posts