douwen5681 2019-02-18 21:33
浏览 51

Laravel使用数据库进行本地化:“unserialize():偏移31位错误,54个字节

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器