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

时间:2018-07-11 10:47:11

标签: php laravel laravel-5 eloquent laravel-5.2

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

我的逻辑是: 我将搜索结果传递到前端(index.blade.php),并将该值存储在一个隐藏的输入字段中,当我单击CSV导出链接时,我会将这个隐藏的输入传递给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 ▶}
  ]
}

提前谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您需要“反序列化”。

您可以使用诸如https://jmsyst.com/libs/serializer之类的包,您可以将其提供json数据,并将其反序列化为所需的类。或..

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

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

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

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

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

继续操作,直到构建了对象。

然后将其转换为集合,您可以调用Laravels集合助手

$memberMasterCollection = collect([$memberMaster]);

这显然可以达到最佳,但它为您提供了基础知识。

OR

您可以使其保持非常简单。而不是整个对象,而是通过ID传递给表单。

然后只需将您的ID发布到表单即可。

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