【问题标题】:How to add array inside select statement of mysql in php(laravel)?如何在php(laravel)中mysql的select语句中添加数组?
【发布时间】:2018-07-07 04:22:57
【问题描述】:

我正在尝试在 laravel 的 select 语句中添加数组, 这是示例数据,请查看以下片段,

    $message =["messages"=>[ 
                            ["attachment"=>
                              ["type"=>"template","payload"=>
                                ["template_type"=>"list","top_element_style"=>"large","elements"=>
                                  [
                                    ["title"=>"test","image_url"=> "https://cdn-images-1.medium.com/max/375/0*sepg7sXXe3zcmGvZ.png","subtitle"=>"Soft white cotton t-shirt is back in style","buttons" =>
                                      [
                                        ["type"=>"web_url","url"=>"https://petersapparel.parseapp.com/buy_item?item_id=101","title"=>"buy item"]
                                      ]
                                    ]
                                    ,
                                    ["title"=>"test","image_url"=> "https://cdn-images-1.medium.com/max/375/0*sepg7sXXe3zcmGvZ.png","subtitle"=>"Soft white cotton t-shirt is back in style","buttons" =>
                                       [
                                         ["type"=>"web_url","url"=>"https://petersapparel.parseapp.com/buy_item?item_id=101","title"=>"buy item"]]
                                     ]

                                    ]
                                ]     
                            
                              ]
                           ]
                          ]
              ]; 

当我从 laravel 的数据库中提取时,我只能做直到字幕,我不能做按钮部分

  ["type"=>"web_url","url"=>"https://petersapparel.parseapp.com/buy_item?item_id=101","title"=>"buy item"]

,这是我的尝试,

    $message =["messages"=>[ 
                            ["attachment"=>
                              ["type"=>"template","payload"=>
                                ["template_type"=>"list","top_element_style"=>"large","elements"=>
                                  [
                                    Fabric::orderBy('id', 'DESC')->where('title', 'LIKE', "%$search_term%")
                                     ->select('title','postdate as image_url',"company_name as subtitle"
                                      )->get()
                                    ]
                                ]     
                            
                              ]
                           ]
                          ]
              ]; 

我为每个按钮数组字段都有另一个字段,不知道如何在 select 语句中添加。任何帮助将不胜感激。

【问题讨论】:

    标签: php mysql arrays laravel


    【解决方案1】:

    Eloquent 模型上的 get 方法返回一个集合,但您似乎正在寻找一个数组。

    所以只需像这样修改你的代码:

    Fabric::orderBy('id', 'DESC')->where('title', 'LIKE', "%$search_term%")
           ->select('title','postdate as image_url',"company_name as subtitle")
           ->get()->toArray();
    

    【讨论】:

    • 嗨兄弟,我想做最后的部分,按钮和数组列表,你能看到示例数据有字幕后面的数据吗,我的代码直到字幕才完成。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2013-09-17
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多