Laravel and a While Loop

I’m new to Laravel and at the moment I have a piece of code in a Controller which without the while loop it works, it retrieves my query from the database.

public function dash($id, Request $request) {
        $user = JWTAuth::parseToken()->authenticate();
        $postdata = $request->except('token');

        $q = DB::select('SELECT * FROM maps WHERE user_id = :id', ['id' => $id]);

        if($q->num_rows > 0){
            $check = true;

            $maps = array();
            while($row = mysqli_fetch_array($q)) {
                $product = array(
                    'auth' => 1,
                    'id' => $row['id'],
                    'url' => $row['url'],
                    'locationData' => json_decode($row['locationData']),
                    'userData' => json_decode($row['userData']),
                    'visible' => $row['visible'],
                    'thedate' => $row['thedate']
                );
                array_push($maps, $product);
            }
        } else {
            $check = false;
        }

        return response()->json($maps);
    }

I am trying to loop through the returned data from $q and use json_decode on 2 key/val pairs but I can’t even get this done right.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s