I created a Survey website, and then I want to use dynamic localization, I need to store everything in two languages in the database. So if the user chose "EN" at the right top corner it should display everything in English. I used unserialize and serialize, but I keep receiving this error message: "unserialize(): Error at offset 31 of 54 bytes..
To make it easy to understand this is my view to create a new survey, as you can see I will have 4 fields two for EN and two for AR.
<form method="POST" action="create" id="boolean">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
@foreach(LaravelLocalization::getSupportedLocales() as $key => $value)
<div class="input-field col s12">
<input name="title[{{ $key }}]" id="title" type="text" placeholder="title in {{ $value['name']}}">
<label for="title">Survey Title</label>
</div>
<div class="input-field col s12">
<textarea name="description[{{ $key }}]" id="description" class="materialize-textarea" placeholder="description in {{ $value['name']}}"></textarea>
<label for="description">Description</label>
</div>
@endforeach
<div class="input-field col s12">
<button class="btn waves-effect waves-light">Submit</button>
</div>
</div>
</form>
In SurveyController.php I used serialize when storing the values as an array in two languages.
public function create(Request $request, Survey $survey)
{
$survey->title= serialize($request->title);
$survey->description= serialize($request->description);
$survey ->save();
return Redirect::to("/survey/{$survey->id}");
}
So, the data will be stord like this:
a:2:{s:2:"en";s:11:"Education";s:2:"ar";s:16:"التعليم";}
I know that I need to unserialize the array to help me display one language depends on what user already choose for language.
This is the view that will display the title of the survey.
@forelse ($surveys as $survey)
<li class="collection-item">
<div>
{{ link_to_route('detail.survey', unserialize ($survey->title)[LaravelLocalization::getCurrentLocale()], ['id'=>$survey->id])}}
<a href="survey/view/{{ $survey->id }}" title="Take Survey" class="secondary-content"><i class="material-icons">send</i></a>
<a href="survey/{{$survey->id}}/edit" title="Edit Survey" class="secondary-content"><i class="material-icons">edit</i></a>
<a href="survey/answers/{{ $survey->id }}" title="View Survey Answers" class="secondary-content"><i class="material-icons">textsms</i></a>
</div>
</li>
@empty
<p class="flow-text center-align">Nothing to show</p>
@endforelse
</ul>
I changed this line :
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}}
to this:
{{ link_to_route('detail.survey', unserialize ($survey->title)[LaravelLocalization::getCurrentLocale()], ['id'=>$survey->id])}}
but didn't work this is the error message: (https://i.imgur.com/jYkDa9u.png). If I use this :
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}
^ I will get this as result: (https://i.imgur.com/TxEcxa2.png).
So, what I need to do?. I want to be able to click the language that I want and everything will be translated be retrieving the language from the array in the database.