I am building an application where there is a column of data that I am serializing to store in the database because I found no other way to store the data as one object. My form where the data is input checkboxes looks like this.
<div class="form-group form-check col-sm-6">
</br>
<label for="careplan">Care Plan</label></br>
<input type ="checkbox" name="careplan[]" value="nursecallweekly"> Nurse call weekly</br>
<input type ="checkbox" name="careplan[]" value="nursecallbiweekly"> Nurse call bi-monthly</br>
<input type ="checkbox" name="careplan[]" value="nursecallmonthly"> Nurse call monthly</br>
<input type ="checkbox" name="careplan[]" value="weightcheckdaily"> Weight check daily</br>
<input type ="checkbox" name="careplan[]" value="weightcheckweekly"> Weight check weekly</br>
<input type ="checkbox" name="careplan[]" value="lowerextremity"> Lower extremity edema status</br>
<input type ="checkbox" name="careplan[]" value="dietaryreview"> Dietary review</br>
<input type ="checkbox" name="careplan[]" value="fluidintake"> Fluid intake review</br>
<input type ="checkbox" name="careplan[]" value="bloodpressure"> Blood pressure reading daily</br>
<input type ="checkbox" name="careplan[]" value="bloodpressureweekly"> Blood pressure reading weekly</br>
<input type ="checkbox" name="careplan[]" value="hypertensive"> Hypertensive symptoms check</br>
<input type ="checkbox" name="careplan[]" value="ptinrweekly"> PT/INR weekly review</br>
</div>
The care plan has multiple items that can be selected. I grouped them into an array and serialized the array using.
//Create new Patient information
$patient = new Patient;
$patient->first_name = $request->input('first_name');
$patient->last_name = $request->input('last_name');
$patient->dob = $request->input('dob');
$patient->contact_one = $request->input('contact_one');
$patient->contact_two = $request->input('contact_two');
$patient->care_giver_one = $request->input('care_giver_one');
$patient->care_giver_two = $request->input('care_giver_two');
$patient->enrollment_reason = $request->input('enrollment_reason');
$care_plan = $request->input('careplan');
$cp = serialize($care_plan);
$patient->careplan = $cp;
$patient->save();
The image at the top is the view. As you can see, the serialized data is returned. I have looked through this forum and none of the posted suggestions worked.
This page doesn't even come close to what I am trying to do.
https://laravel.com/docs/5.8/eloquent-serialization#date-serialization
This was close but did not share what was done that worked or I don't understand what worked.
https://laracasts.com/discuss/channels/laravel/problem-to-unserialize-my-returned-value
UPDATE: SOLUTION THAT WORKED FOR ME
I made a couple of changes that helped and I am 98% of the way there.
This post helped me. Split string in Laravel Framework
I changed this line from this
$care_plan = $request->input('careplan');
To this
$patient->careplan = json_encode($request->input('careplan'));
That got me a return from the database of this.
["nursecallweekly","nursecallbiweekly","nursecallmonthly"]
and I used this nested loop to parse the string.
@foreach(explode('","', $patient->careplan) as $plan)
<li>{{$plan = Str::after($plan,'["')}}</li>
@endforeach
What displays like this.
nursecallweekly
nursecallbiweekly
nursecallmonthly"]
My only issue now is the trailing bracket.
Solution to the trailing brace
@foreach(explode('","', $patient->careplan) as $plan)
@if(Str::contains($plan, '['))
<li>{{ Str::after($plan,'["') }}</li>
@else
<li>{{ Str::before($plan,'"]') }}</li>
@endif
@endforeach