gpt4 book ai didi

javascript - 查询结果到json laravel 5

转载 作者:行者123 更新时间:2023-11-27 23:24:33 28 4
gpt4 key购买 nike

我希望能够在应用程序 View 的级联下拉列表中使用 Laravel 的查询结果。

首先,我只想将查询结果转换为可以在 Laravel 下拉列表中使用的内容。

我有以下代码:

$accounts = json_encode(DB::table('accounts')->join('Bookmakers', 'bookmakers.id', '=', 'accounts.bookmaker_id')->get());

这会产生: (来自 dd($accounts))

"[{"id":"1","customer_id":"1","bookmaker_id":"1","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"William Hill","type":"Bookmaker","commission":"0"},{"id":"2","customer_id":"1","bookmaker_id":"2","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"Betfred","type":"Bookmaker","commission":"0"},{"id":"3","customer_id":"1","bookmaker_id":"3","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"Skybet","type":"Bookmaker","commission":"0"},{"id":"1","customer_id":"2","bookmaker_id":"1","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"William Hill","type":"Bookmaker","commission":"0"},{"id":"2","customer_id":"2","bookmaker_id":"2","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"Betfred","type":"Bookmaker","commission":"0"},{"id":"3","customer_id":"2","bookmaker_id":"3","balance":"25","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"Skybet","type":"Bookmaker","commission":"0"},{"id":"6","customer_id":"2","bookmaker_id":"6","balance":"100","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"DS","type":"Exchange","commission":"2"}]"

这看起来基本上没问题。

我正在尝试将其分配给我认为的 JavaScript 变量:

var accountData = {{ $accounts }};

但结果是这样的......

var accountData =     [{"id":"1","customer_id":"1","bookmaker_id":"1","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"William Hill","type":"Bookmaker","commission":"0"},{"id":"2","customer_id":"1","bookmaker_id":"2","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"Betfred","type":"Bookmaker","commission":"0"},{"id":"3","customer_id":"1","bookmaker_id":"3","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"Skybet","type":"Bookmaker","commission":"0"},{"id":"1","customer_id":"2","bookmaker_id":"1","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"William Hill","type":"Bookmaker","commission":"0"},{"id":"2","customer_id":"2","bookmaker_id":"2","balance":"0","profit":"0","created_at":"2016-01-28 17:44:33","updated_at":"2016-01-28 17:44:33","name":"Betfred","type":"Bookmaker","commission":"0"},{"id":"3","customer_id":"2","bookmaker_id":"3","balance":"25","profit":"0","created_at":"2016-01-28 17:44:33","updated_ ...etc

谁能告诉我应该做什么才能将数据转换为我认为的 json 格式?

最佳答案

这应该有帮助:

var accountData = {!! $accounts !!};

当使用{!! $var !!} Blade 引擎不会转义您的数据。

关于javascript - 查询结果到json laravel 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069158/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com