How to store multiple Array values in one column in PHP

Posted by

In this tutorial, I’m going to learn how to store multiple data in one field. so follow this tutorial in this tutorial we have mentioned it in a very easy way.

Step 1: In Blade Page get data in a foreach().

@foreach($user_slug_summary as $index => $item)
                    <input type="hidden" value="{{ $item->slug }}" name="influencer_name[]" id="influencer_name{{ $index }}" />
                    <input type="hidden" value="{{ $item->influencer_email }}" name="influencer_email[]" id="influencer_email{{ $index }}" />
                    @endforeach

Step 2: In the Controller,

Store function:-

Log::info("success m kya aa rha h" . json_encode($request->influencer_email));

$payment = new paypal();
$influencerEmails = $request->session()->get('influencer_email', []);
                $influencerNames = $request->session()->get('influencer_name', []);
                if (!empty($influencerEmails)) {

                    $influencers = [];
                    for ($i = 0; $i < count($influencerEmails); $i++) {
                        $influencers[] = [
                            'influencer_email' => $influencerEmails[$i],
                            // 'influencer_name' => $influencerNames[$i]
                        ];
                    }
                    $payment->influencer_email = json_encode($influencers);
                }
                if (!empty($influencerNames) ) {

                    $influencersname = [];
                    for ($i = 0; $i < count($influencerNames); $i++) {
                        $influencersname[] = [
                            // 'influencer_email' => $influencerEmails[$i],
                            'influencer_name' => $influencerNames[$i]
                        ];
                    }
                    $payment->influencer_name = json_encode($influencersname);
                }

                $payment->save();

Hopefully, It will help You !!!!!!