我试图将数据库中的结果作为数组传递给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();
返回变量视图:
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]
我该如何解决这个问题并获得一个有效的阵列?
答案 0 :(得分:1)
试试这个:
$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) }};
答案 1 :(得分:0)
@ezw @It全是你的
我弄清楚问题是什么......我完全错过了我的传球:
['results.user_id', '=', $user_id],
['results.test_id', '=', $request->get('test_id')],
通过AJAX和我正在运行的那些数据
var test = {!! json_encode($test_min_max_list->toArray()) !!};
之前的这个变量,这就是为什么它是空的...... 对不起,感到困惑。