du155305 2018-07-11 10:47
浏览 64
已采纳

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

I am working on a project using Laravel 5.2. I have a search and CSV export functions. I want to export the latest search result as a CSV file if I click on CSV export link.

My logic is : I pass the search result to front-end (index.blade.php) and store the value in a hidden input field and when I click CSV export link, I will pass this hidden input to the CSV export controller and do the printing operation.

My problem is: how can I change these data format(the hidden input data)

[
  {
    "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"
  }
]

To the following data format (Laravel Query format)

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 ▶}
  ]
}

Thank you in advance!!

  • 写回答

2条回答 默认 最新

  • dsaob80228 2018-07-11 11:10
    关注

    You need to 'deserialize'.

    You could use a package like https://jmsyst.com/libs/serializer which you can supply your json data to, and deserialize it into the class you require. or..

    First of all the data in your form is JSON. You will need to decode it into an object or an array by calling

    json_decode($yourData);
    

    Once this is decoded you will have an object containing your data.

    You need to transform this into your object e.g.

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

    And continue until you've built the object.

    To then turn this into a collection you can call Laravels collection helper

    $memberMasterCollection = collect([$memberMaster]);
    

    This obviously can be more optimal but it gives you the basics.

    OR

    You could keep it really simple. Instead of the whole object just pass though the ID to your form.

    Then just post your id up with the form.

    Use a database call to get your object from the DB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog