【问题标题】:unserialize error from a column in database从数据库中的列中反序列化错误
【发布时间】:2017-09-10 16:14:38
【问题描述】:

自从有一天我想访问未序列化的列时,我的视图出现了异常。这真的很奇怪,因为在某些页面中它正在工作,而在另一些页面中我得到一个错误

希望有人可以帮助我.. 我不知道为什么有时它会起作用,有时却不起作用。我还测试了 base64_encode() 和 base64_decode() 但我现在从我的不同记录中得到所有视图的错误

反序列化错误偏移

这里是我创建新订单时的控制器

     $order = new Order();
     $order->cart = serialize($cart);
     ...
     $order->save();

当我反序列化时,现在是我的控制器

public function show($id)
  {
      $order = Order::findOrFail($id);
      $order->cart = unserialize($order->cart);
      $order->cart->totalPrice;
      $prix_total = $order->cart->totalPrice;
      $structure_id = $order->structure_id;
      $structure = Structure::where('id' , '=' , $structure_id)->first();
      $federation = Structure::where('id' , '1')->first();

      return view('cotisation_structure/show' , compact('prix_total', 'order' , 'structure' , 'federation'));

  }

现在我的视图显示我的“购物车”列中的记录:

@foreach($order->cart->items as $item)
                       <tr>
                           @if(Auth::user()->isFederation())
                               <td><input type="checkbox" name="checkbox[]" value="{{$item['item']->id}}"></td>
                           @endif
                           <td><a href="{!! route('licencie.show', $item['item']->id) !!}">{{$item['item']->num_licence}}</a></td>
                           <th>{{$item['item']->lb_nom}}</th>
                           <th>{{$item['item']->lb_prenom}}</th>
                           <th>{{$item['item']->structure->nom_structure}}</th>
                               <td>
                                   @if($item['item']->lb_tricolore != null)
                                       {{$item['item']->lb_activite_tricolore}} - {{$item['item']->lb_tricolore}}
                                   @else
                                       {{$item['item']->activite_licencie->lb_activite}}
                                   @endif
                               </td>
                           <th>{{$item['item']->saison->lb_saison}}</th>
                           <th>{{$order->payment_method}}</th>
                           <th>{{$item['price']}} € </th>
                           <td>{{Carbon\Carbon::parse($order->date_achat)->format('d/m/Y')}}</td>
                       </tr>
                    @endforeach

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您可能将序列化字段存储在 string 类型的数据库表字段中,并且它在 255 个字符处被剥离。

    为了进一步调试,我建议将序列化数据保存到日志中,然后在恢复时保存。在这种情况下,您可以比较您保存的数据是否与取出的数据相同。

    【讨论】:

    • 感谢您的回复!我使用“文本”作为列。这种类型的字段有长度限制吗?
    • 我应该使用长文本吗?
    • 取决于你的序列化数据大小。 TEXT 65,535 (216-1) 字节 = 64 KiB MEDIUMTEXT 16,777,215 (224-1) 字节 = 16 MiB LONGTEXT 4,294,967,295 (232-1) 字节 = 4 GiB
    • 为了确定,我改成了 LONGTEXT。现在我没有更多的例外。它似乎工作。非常感谢您的帮助先生!!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    相关资源
    最近更新 更多