如何将字符串数据改为Laravel查询格式

How to change the string data to Laravel Query format

我正在使用 Laravel 5.2 进行一个项目。我有搜索和 CSV 导出功能。如果单击 CSV 导出 link,我想将最新的搜索结果导出为 CSV 文件。

我的逻辑是: 我将搜索结果传递给前端 (index.blade.php) 并将值存储在一个隐藏的输入字段中,当我单击 CSV 导出 link 时,我会将这个隐藏的输入传递给 CSV 导出控制器并执行打印操作。

我的问题是:如何更改这些数据格式(隐藏的输入数据)

[
  {
    "memberId": 9,
    "area_id": 2,
    "status_id": 0,
    "reference_id": 0,
    "productname": null,
    "picaluminum": null,
    "companyName": "てすと",
    "name": "てすと会社",
    "job": 3,
    "relationJob": null,
    "subJobCategory": null,
    "busyo": null,
    "eMail": "y_ikegami@gmail.com",
    "password": "password",
    "zip1": "530",
    "zip2": "0001",
    "pref": 27,
    "prefStr": "大阪府",
    "address1": "大阪市北区梅田2-2-2",
    "address2": "20F",
    "tel1": "06",
    "tel2": "1111",
    "tel3": "1111",
    "fax1": "06",
    "fax2": "9999",
    "fax3": "9999",
    "siteUrl": "http://www.google/",
    "medium": "|1|",
    "pdf": "|1|",
    "created_at": "2015-07-27 18:23:28",
    "updated_at": null
  },
  {
    "memberId": 5275,
    "area_id": 2,
    "status_id": 3,
    "reference_id": 4,
    "productname": "yonascare",
    "picaluminum": "",
    "companyName": "ddd",
    "name": "Test Queserser",
    "job": null,
    "relationJob": null,
    "subJobCategory": null,
    "busyo": null,
    "eMail": null,
    "password": null,
    "zip1": null,
    "zip2": null,
    "pref": null,
    "prefStr": null,
    "address1": "zzzzzz",
    "address2": null,
    "tel1": null,
    "tel2": null,
    "tel3": null,
    "fax1": null,
    "fax2": null,
    "fax3": null,
    "siteUrl": null,
    "medium": null,
    "pdf": null,
    "created_at": "2018-07-06 06:42:20",
    "updated_at": "2018-07-06 06:42:20"
  },
  {
    "memberId": 5279,
    "area_id": 2,
    "status_id": 1,
    "reference_id": 1,
    "productname": "yonascare",
    "picaluminum": "hello",
    "companyName": "yonas3",
    "name": "Test Queserser",
    "job": null,
    "relationJob": 1,
    "subJobCategory": 6,
    "busyo": null,
    "eMail": null,
    "password": null,
    "zip1": null,
    "zip2": null,
    "pref": null,
    "prefStr": null,
    "address1": "zzzzzz",
    "address2": null,
    "tel1": null,
    "tel2": null,
    "tel3": null,
    "fax1": null,
    "fax2": null,
    "fax3": null,
    "siteUrl": null,
    "medium": null,
    "pdf": null,
    "created_at": "2018-07-06 06:57:12",
    "updated_at": "2018-07-06 07:04:51"
  }
]

为以下数据格式(Laravel查询格式)

Collection {#403 ▼
  #items: array:3 [▼
    0 => MemberMaster {#399 ▼
      #table: "memberMasternewdata"
      #primaryKey: "memberId"
      #fillable: array:10 [▶]
      #connection: null
      #keyType: "int"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:31 [▼
        "memberId" => 9
        "area_id" => 2
        "status_id" => 0
        "reference_id" => 0
        "productname" => null
        "picaluminum" => null
        "companyName" => "てすと"
        "name" => "てすと会社"
        "job" => 3
        "relationJob" => null
        "subJobCategory" => null
        "busyo" => null
        "eMail" => "y_ikegami@gmail.com"
        "password" => "password"
        "zip1" => "530"
        "zip2" => "0001"
        "pref" => 27
        "prefStr" => "大阪府"
        "address1" => "大阪市北区梅田2-2-2"
        "address2" => "20F"
        "tel1" => "06"
        "tel2" => "1111"
        "tel3" => "1111"
        "fax1" => "06"
        "fax2" => "9999"
        "fax3" => "9999"
        "siteUrl" => "http://www.google.com/"
        "medium" => "|1|"
        "pdf" => "|1|"
        "created_at" => "2015-07-27 18:23:28"
        "updated_at" => null
      ]
      #original: array:31 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => MemberMaster {#400 ▶}
    2 => MemberMaster {#401 ▶}
  ]
}

提前致谢!!

您可以使用 json_encode($collection) 函数将您的 Collection 转换为 json 编码字符串。

你需要'deserialize'.

您可以使用像 https://jmsyst.com/libs/serializer 这样的包,您可以向其中提供 json 数据,并将其反序列化为您需要的 class。或者..

首先,您表单中的所有数据是 JSON。您需要通过调用

将其解码为对象或数组
json_decode($yourData);

解码后,您将拥有一个包含您的数据的对象。

您需要将其转换为您的对象,例如

$jsonObject = json_decode($jsonData);
$memberMaster = new MemberMaster();
$memberMaster->memberId = $jsonObject->memberId;

然后继续,直到您构建了对象。

然后调用 Laravels collection helper 将它变成一个集合

$memberMasterCollection = collect([$memberMaster]);

这显然可以更优化,但它为您提供了基础知识。

你可以保持简单。只需将 ID 传递给您的表单,而不是整个对象。

然后只需post填写您的身份证件即可。

使用数据库调用从数据库中获取对象。