Debug School

rakesh kumar
rakesh kumar

Posted on

How to get data from json string using laravel

Let's break down the provided code:

Querying influencer_admin_id from the Paytm Model:

$mys_order = Paytm::where('id', $id)->value('influencer_admin_id');
Enter fullscreen mode Exit fullscreen mode
    Log::info("mutryuu");
        Log::info($mys_order);
[2024-03-22 10:12:29] local.INFO: [84,22] 
Enter fullscreen mode Exit fullscreen mode

This line queries the influencer_admin_id column from the Paytm model where the id column matches the given $id. The value() method returns only the value of the specified column.

Decoding JSON String into an Array:

$dataArray = json_decode($mys_order);
Enter fullscreen mode Exit fullscreen mode

Assuming that $mys_order is a JSON string (e.g., "[84,22]"), this line decodes the JSON string into a PHP array using json_decode(). This will convert the JSON string into an array, where each value from the JSON string becomes an element in the array.

Iterating Over the Array:

foreach ($dataArray as $value) {   
    $userName = Addprofile::where('user_id', $value)->value('user_name');
    $userNames[] = $userName;
}
Enter fullscreen mode Exit fullscreen mode
   Log::info($userNames);
[2024-03-22 10:12:29] local.INFO: array (
  0 => 'Ashwani',
  1 => 'sourav',
) 
Enter fullscreen mode Exit fullscreen mode

This loop iterates over each element in the $dataArray. For each element ($value), it queries the user_name from the Addprofile model where the user_id matches the current value ($value). The retrieved user_name is then added to the $userNames array.

Assigning influencer_name to the Model:

if ($users_order) {
    $users_order->influencer_name = $userNames;
}
Enter fullscreen mode Exit fullscreen mode

Assuming $users_order is an instance of the Paytm model, this block of code assigns the $userNames array (containing the influencer names) to the influencer_name property of the $users_order model instance.

Overall, this code retrieves influencer IDs from the Paytm model, fetches their corresponding names from the Addprofile model, and assigns these names to the influencer_name property of the $users_order model instance.

Full code

public function view_order($id){
        $users_order = Paytm::find($id);
        Log::info('order this function accounts_admin_edit '.$users_order);
 $mys_order = Paytm::where('id',$id)->value('influencer_admin_id');
 $dataArray = json_decode($mys_order);
        $userNames = [];

     foreach ($dataArray as $value) {   
    $userName = Addprofile::where('user_id', $value)->value('user_name');

                $userNames[] = $userName;

         }
 if ($users_order) {

            $users_order->influencer_name = $userNames;
        }

        if (is_null($users_order)) {
            $response = [
                'success' => false,
                'data' => $users_order,
                'message' => 'Order not found.'
            ];
            return response()->json($response, 404);
        } else {
            $response = [
                'success' => true,
                'data' =>new OrderResourceorder($users_order),
                'message' => 'user retrieved successfully.'
            ];
                return response()->json($response, 200);
        }
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)