Survey Web サイトを作成した後、動的なローカリゼーションを使用したいので、すべてを 2 つの言語でデータベースに保存する必要があります。したがって、ユーザーが右上隅で「EN」を選択すると、すべてが英語で表示されます。unserialize と serialize を使用しましたが、「unserialize(): Error at offset 31 of 54 bytes..」というエラー メッセージが引き続き表示されます。
理解を容易にするために、これは新しいアンケートを作成する私の見解です。ご覧のとおり、EN 用に 2 つ、AR 用に 2 つの 4 つのフィールドがあります。
<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>
SurveyController.php では、2 つの言語で値を配列として格納するときにシリアライズを使用しました。
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}");
}
したがって、データは次のように保存されます。
a:2:{s:2:"en";s:11:"Education";s:2:"ar";s:16:"التعليم";}
ユーザーが選択した言語に応じて、1 つの言語を表示できるように配列をシリアル化解除する必要があることはわかっています。
これは、アンケートのタイトルを表示するビューです。
@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>
この行を変更しました:
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}}
これに:
{{ link_to_route('detail.survey', unserialize ($survey->title)[LaravelLocalization::getCurrentLocale()], ['id'=>$survey->id])}}
これはエラーメッセージです: ( https://i.imgur.com/jYkDa9u.png ). 私がこれを使用する場合:
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}
^ 結果としてこれを取得します: ( https://i.imgur.com/TxEcxa2.png ).
それで、私は何をする必要がありますか?必要な言語をクリックできるようにしたいのですが、データベース内の配列から言語を取得することですべてが翻訳されます。