Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel 5视图中取消序列化一列数据的正确方法是什么_Php_Laravel_Eloquent - Fatal编程技术网

Php 在laravel 5视图中取消序列化一列数据的正确方法是什么

Php 在laravel 5视图中取消序列化一列数据的正确方法是什么,php,laravel,eloquent,Php,Laravel,Eloquent,我正在构建一个应用程序,其中有一列数据正在序列化以存储在数据库中,因为我没有找到其他方法将数据存储为一个对象。我的表单中的数据输入复选框如下所示 <div class="form-group form-check col-sm-6"> </br> <label for="careplan">Care Plan</label></br> <input type ="checkb

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

    <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>
   nursecallweekly
   nursecallbiweekly
   nursecallmonthly"]
顶部的图像是视图。如您所见,将返回序列化数据。我浏览了这个论坛,没有一条建议有效

这一页甚至没有接近我想要做的

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

更新:对我有效的解决方案

我做了一些有帮助的改变,我98%都做到了

这篇文章帮助了我。

我把这行改成了这行

  $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
我现在唯一的问题是后面的括号

后支撑的解决方案

你应该用它来做这件事

在数据库中使用JSON列,我们称之为careplan,然后在模型中执行以下操作:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Patient extends Model
{
    // ...

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

    // ...
}
然后你会看到:

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

已返回阵列。

有什么问题吗?aynber,unserialize不适合我,或者无法找到unserialize$patient->careplan的位置。这会造成很大的错误。所以我不得不回到数据是如何存储的。我将改为json_encode,并将下面的建议作为使用mutator进行cast的答案。当字符串返回时,所有其他步骤都被引入以呈现字符串。谢谢。我们做的是向apps>Http>Models>CarePlan.php添加第一个代码。然后在PatientController.php中,我编写了它,如第二个代码段所示。当我试图救一个新病人时,发生了一个错误。数组到字符串转换错误。我不知道最后一部分该怎么办。它应该住在哪里?所以,我有一个想法,因为作为模型的app>Patient.php基本上是空的。我将第一段代码移到这里。然后我得到一个新的错误,htmlspecialchars期望参数1是字符串,数组给定视图:/var/www/html/eastccm/resources/views/home.blade.phpI从患者模型中删除了第一个代码块,并且主页没有加载。我可以看到它确实保存了数组。但是,主页将不会加载该代码。第三段代码到哪里去了?@user1794918很抱歉我回复晚了。第三个代码片段只是一个示例,说明值应该被转换回数组,它根本不需要被包含。第二条注释htmlspecialchars中报告的错误是因为元素现在是一个数组,所以需要循环遍历它以获取每一条数据。在您的第三条评论中:如果删除第一个代码段,那么它将不会被转换回数组,这就是它加载页面的原因,因为它是一个字符串json。我必须对每个代码段进行嵌套,并且在尝试时只会收到一条错误消息。
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Patient extends Model
{
    // ...

    protected $casts = [
        'careplan' => '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);