-1

I am not able to set my javascript variable for my blade file:

$filterCoupons = data;

I want to use this variable in a loop.

thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
Jainam Shah
  • 204
  • 1
  • 9
  • https://stackoverflow.com/questions/33489931/how-to-use-laravel-blade-in-a-script-file Try this – anwerj Jun 26 '19 at 07:28
  • @anwerj i want to use variable data inside blade file, link is for the blade file variable into js – Jainam Shah Jun 26 '19 at 07:33
  • It is not possible to do this in that way. Where are coming from your `data`? Please, add the relevant code in your post. – Kévin Bibollet Jun 26 '19 at 07:35
  • From javascript to PHP, use ajax request(or whatever request format you like). From PHP to javascript, you can have interpolation `{{}}` to assign the value. – nice_dev Jun 26 '19 at 07:37
  • js js Javascript:- var data = coupons.filter(e => e.service_type_id == id || e.coupon_type == 1 || e.coupon_type == 4); laravel blade file @foreach($filterCoupons as $key => $value) – Jainam Shah Jun 26 '19 at 07:45

1 Answers1

1

You can do this

<script>
    let data = {{json_encode($filterCoupons)}}
</script>

Or you can put all filters into array

var data = [ {{ implode(",", $filterCoupons )}} ];
Faid
  • 554
  • 1
  • 5
  • 18
  • 1
    `You cannot assign a php variable to a javascript variable` maybe you meant the other way round. Also, the values filled in that `foreach` won't be comma separated. You could better use `{{ implode(",",$filterCoupons ) }}` instead of looping. – nice_dev Jun 26 '19 at 07:45
  • 3
    @vivek_23 You are true, I just want him to know the idea not only copy and paste the code, But any way I'll Edit it. – Faid Jun 26 '19 at 08:05