I store a few checkboxes as array in Laravel, but I get an error on Foreach. Can someone help me?
in model
public function setHandServiceAttribute($value)
{
$this->attributes['handservice'] = json_encode($value);
}
public function getHandServiceAttribute($value)
{
return $this->attributes['handservice'] = json_decode($value);
}
in checkbox
<div >
<input type="radio" id="1" value="کاشت" name="handservice[]">
<label for="1">کاشت</label>
</div>
<div >
<input type="radio" id="2" value="کاور" name="handservice[]">
<label for="2">کاور</label>
</div>
handservice is column of reservation table in database with value like this after store
["\u062a\u0631"]
and in view
@forelse($reservation->handservice as $hand)
{{ $hand }},
@empty
nothing
@endforlse
error
json_decode(): Argument #1 ($json) must be of type string, array given
I used this article but I get an error when one of the columns handservie is empty handservice in nullable
https://hackthestuff.com/article/how-to-store-multiple-checkbox-value-in-database-in-laravel8
CodePudding user response:
It finally worked
in model
public function getHandServiceAttribute($value) {
return json_decode($value);
}
in view
@if($reservation->handservice != null)
@forelse($reservation->handservice as $hand)
{{$hand}},
@empty
نا مشخص
@endforelse
@else
<p >انتخاب نشده</p>
@endif
thanks all
CodePudding user response:
You shouldn't try to assign something in a getter.
public function getHandServiceAttribute()
{
return json_decode($this->attributes['handservice']);
}
