gpt4 book ai didi

javascript - Laravel - 将 php 变量作为数组传递给 JS

转载 作者:行者123 更新时间:2023-12-03 02:25:20 24 4
gpt4 key购买 nike

我试图将结果从数据库传递到 js 变量作为数组:

        $test_min_max_list = TestsMinMax
::join('results', 'tests_min_max.id', '=', 'results.test_result_id')
->select('created_at','min','avg','max')
->where([
['results.user_id', '=', $user_id],
['results.test_id', '=', $request->get('test_id')],
])->get();

变量的返回 View :

    return view('results', [
'test_min_max_list' => $test_min_max_list
]);

页面结果:

{{$test_min_max_list}}

->

[{"created_at":"2018-01-13 22:47:17","min":0.29999999999999999,"avg":0.40000000000000002,"max":0.53000000000000003},{"created_at":"2018-01-13 22:48:58","min":0.29999999999999999,"avg":0.76300000000000001,"max":1.972},{"created_at":"2018-01-13 22:54:51","min":0.93899999999999995,"avg":2.0409999999999999,"max":3.3500000000000001},{"created_at":"2018-01-14 14:38:31","min":0.36699999999999999,"avg":0.39400000000000002,"max":0.42299999999999999},{"created_at":"2018-01-14 18:27:06","min":0.29699999999999999,"avg":0.44900000000000001,"max":0.90000000000000002},{"created_at":"2018-02-17 13:07:04","min":0.29499999999999998,"avg":0.30599999999999999,"max":0.34000000000000002},{"created_at":"2018-02-18 11:29:35","min":0.35999999999999999,"avg":0.38500000000000001,"max":0.40000000000000002}] 

变量结果:

var test = {!! json_encode($test_min_max_list->toArray()) !!};

->

Array [length: 0]

我怎样才能解决这个问题并获得一个工作数组?

最佳答案

试试这个:

 $test_min_max_list = TestsMinMax
::join('results', 'tests_min_max.id', '=', 'results.test_result_id')
->select('created_at','min','avg','max')
->where([
['results.user_id', '=', $user_id],
['results.test_id', '=', $request->get('test_id')],
])->get()->toArray();

在 JS 脚本中

var array = {{ json_encode($test_min_max_list) }};

关于javascript - Laravel - 将 php 变量作为数组传递给 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977738/

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