shpathuala
shpathuala

Reputation: 53

how to echo key name and key value from json decode in laravel blade

I am new to laravel

I have a form. I get data from user and save it as json encoded string in DB. This is my laravel controller code.

public function order_requirements(Request $request)
 {   
     // Get the currently authenticated user...
     $user = Auth::user();
     $order_number = request('order_number');
     $orderData = Orders::whereOrder_number($order_number)->first();
     if($orderData['buyer_id'] == $user['id']){
          
        $orderData->order_requirements = json_encode($request->all());
        $orderData->save();
        return json_encode($request->all());
 
     }       

 }

I can successfully save the incoming order requirements to DB. It is working well. once I saved to DB the data looks like this in DB

{"_token":"dq2zJU03UjxK1F2tAaXSu8gujUwElv7PkXhLipEY","order_number":"W1622543566X1","logo_name":"dd","logo_slogan":"love chicken","business_description":"chicken","styles":"red color"}

Now I want to show these details in a order page like this

order_number:W1622543566X1,
logo_name:dd,
logo_slogan:love chicken,

like this however I have many different forms, the key name is different form each other.

so my idea is to access key name and then key value. I can access values in laravel blade like this. but I can't get key names.

 @foreach (json_decode($orderData->order_requirements ,true) as $details)
     {{$details}},    
    @endforeach

in above code, $orderData is the returned DB array and the order_requirements field contains the json encoded data I saved.

with foreach loop above, what I get is this.

dq2zJU03UjxK1F2tAaXSu8gujUwElv7PkXhLipEN, W1622543566X1, dd, love chicken, chicken, red color,

but I want to print key name before each value like this

order_number:W1622543566X1,
logo_name:dd,
logo_slogan:love chicken, ...

how can I do this? I have no idea to continue beyond this, please help

Upvotes: 1

Views: 2084

Answers (1)

shaedrich
shaedrich

Reputation: 5715

You need to specify the key in your foreach call and use <br />:

@foreach (json_decode($orderData->order_requirements ,true) as $key => $value)
     {{$key}}:{{$value}},<br />  
@endforeach

Upvotes: 2

Related Questions