首页 > 解决方案 > 如何使用js从我的数据库中获取所有数组值

问题描述

1,这个js只取第一个数组值

2、我用foreach应该取所有的值

3,我很困惑请帮助

<script>

var unit_balance = 
  @foreach ($bike as $value)
    [{{ $value->unit_balance }}];
  @endforeach

function myFunction(){
  var totalLessThan20 = unit_balance.filter(unit => unit <= 20);
  if(totalLessThan20.length) {
   alert("Some of unit left is less than 20. Please restock immediately!");
  }
}
     </script>

标签: javascriptlaravel

解决方案


var unit_balance = JSON.parse('{{ json_encode($bike->unit_balance ) }}');

在Javascript中引用传递(laravel)数组


推荐阅读