【问题标题】:What is the proper way to unserialize one column of data in the laravel 5 view在 laravel 5 视图中反序列化一列数据的正确方法是什么
【发布时间】:2020-01-15 16:07:32
【问题描述】:

我正在构建一个应用程序,其中有一列数据要序列化以存储在数据库中,因为我发现没有其他方法可以将数据存储为一个对象。我的数据输入复选框的表单如下所示。

    <div class="form-group form-check col-sm-6">
        </br>
        <label for="careplan">Care Plan</label></br>
        <input type ="checkbox" name="careplan[]" value="nursecallweekly">   Nurse call weekly</br>
        <input type ="checkbox" name="careplan[]" value="nursecallbiweekly">   Nurse call bi-monthly</br>
        <input type ="checkbox" name="careplan[]" value="nursecallmonthly">   Nurse call monthly</br>
        <input type ="checkbox" name="careplan[]" value="weightcheckdaily">   Weight check daily</br>
        <input type ="checkbox" name="careplan[]" value="weightcheckweekly">   Weight check weekly</br>
        <input type ="checkbox" name="careplan[]" value="lowerextremity">   Lower extremity edema status</br>
        <input type ="checkbox" name="careplan[]" value="dietaryreview">   Dietary review</br>
        <input type ="checkbox" name="careplan[]" value="fluidintake">   Fluid intake review</br>
        <input type ="checkbox" name="careplan[]" value="bloodpressure">   Blood pressure reading daily</br>
        <input type ="checkbox" name="careplan[]" value="bloodpressureweekly">   Blood pressure reading weekly</br>
        <input type ="checkbox" name="careplan[]" value="hypertensive">   Hypertensive symptoms check</br>
        <input type ="checkbox" name="careplan[]" value="ptinrweekly">   PT/INR weekly review</br>
    </div>

护理计划有多个可供选择的项目。我将它们分组为一个数组并使用序列化数组。

    //Create new Patient information
    $patient = new Patient;
    $patient->first_name = $request->input('first_name');
    $patient->last_name = $request->input('last_name');
    $patient->dob = $request->input('dob');
    $patient->contact_one = $request->input('contact_one');
    $patient->contact_two = $request->input('contact_two');
    $patient->care_giver_one = $request->input('care_giver_one');
    $patient->care_giver_two = $request->input('care_giver_two');
    $patient->enrollment_reason = $request->input('enrollment_reason');
    $care_plan = $request->input('careplan');
    $cp = serialize($care_plan);
    $patient->careplan = $cp;
    $patient->save();

顶部的图像是视图。如您所见,返回了序列化的数据。我浏览了这个论坛,但发布的建议都没有奏效。

unserialize data in laravel

这个页面甚至没有接近我想要做的。

https://laravel.com/docs/5.8/eloquent-serialization#date-serialization

这很接近,但没有分享什么是有效的,或者我不明白什么有效。

https://laracasts.com/discuss/channels/laravel/problem-to-unserialize-my-returned-value

更新:适合我的解决方案

我做了一些有帮助的改变,我已经完成了 98% 的工作。

这篇文章帮助了我。 Split string in Laravel Framework

我从这里更改了这一行

  $care_plan = $request->input('careplan');

到这里

 $patient->careplan = json_encode($request->input('careplan'));

这让我从这个数据库返回。

 ["nursecallweekly","nursecallbiweekly","nursecallmonthly"]

我使用这个嵌套循环来解析字符串。

    @foreach(explode('","', $patient->careplan) as $plan)
         <li>{{$plan = Str::after($plan,'["')}}</li>
    @endforeach

这样显示的内容。

   nursecallweekly
   nursecallbiweekly
   nursecallmonthly"]

我现在唯一的问题是尾括号。

尾括号的解决方案

   @foreach(explode('","', $patient->careplan) as $plan)
        @if(Str::contains($plan, '['))
        <li>{{ Str::after($plan,'["') }}</li>
        @else
            <li>{{ Str::before($plan,'"]') }}</li>
        @endif
    @endforeach

【问题讨论】:

  • unserialize 有什么问题?
  • aynber,反序列化对我不起作用,或者无法弄清楚反序列化($patient->careplan)的放置位置。这会引发很大的错误。所以我不得不回到数据的存储方式。我更改为 json_encode 以及下面提出的建议,作为使用 mutator 进行转换的答案。当字符串被带回时,这就是所有这些其他步骤被引入来渲染字符串的地方。

标签: php laravel eloquent


【解决方案1】:

您应该使用Eloquent mutators 来执行此操作。

在您的数据库中使用 JSON 列,我们称之为 careplan,然后在您的模型中执行:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Patient extends Model
{
    // ...

    protected $casts = [
        'careplan' => 'array',
    ];

    // ...
}

所以现在,您将使用 array 元素,不再需要从数据库手动序列化/反序列化它。 Laravel 将在存储时将 array 转换为 json,并在从数据库中获取值时将其转换回 array。所以现在:

$patient = new Patient;
$patient->first_name = $request->input('first_name');
// ...
$patient->careplan = $request->input('careplan'); // Laravel will convert it to json for you
$patient->save();

然后你会看到:

$patient = Patient::find(1);
dd($patient->careplan);

已被转换回array

【讨论】:

  • 谢谢。所做的是将第一个代码添加到应用程序>Http>Models>CarePlan.php。然后在 PatientController.php 中我编写了它,如第二个代码 sn-p 所示。当我尝试保存新患者时,发生了错误。数组到字符串的转换错误。我不知道如何处理最后一部分。那应该住在哪里?
  • 所以,我有一个想法,因为应用程序>Patient.php(这是模型)基本上是空的。我将第一个 sn-p 代码移到那里。然后我得到一个新的错误 htmlspecialchars() Expects parameter 1 to be string, array given (View: /var/www/html/eastccm/resources/views/home.blade.php)
  • 我从 Patient 模型中删除了第一块代码,并且主页确实加载了。我可以看到它确实保存了数组。但是,主页不会加载该代码。第三段代码去哪了?
  • @user1794918 抱歉我的回复晚了。第三个代码 sn -p 只是一个示例,表明应该将值强制转换回数组,根本不需要包含它。您的第二条评论(htmlspecialchars())中报告的错误是因为该元素现在是一个数组,因此您需要遍历它以获取每条数据。在您的第三条评论中:如果您删除第一个代码 sn-p,那么它将不会被转换回数组,这就是它加载页面的原因,因为它是一个字符串 (json)。
  • 我必须为每个都做一个嵌套,当我尝试这样做时,我只会收到一条错误消息。
猜你喜欢
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多