Abdallah Sakre
Abdallah Sakre

Reputation: 915

converting Laravel array values into Javascript variables in blade

I have a foreach in a blade that loops through an array as follows :

@foreach($qwin as $qwinners)

    <div>
     <a href="" >{{$qwinners->title}}</a> 

    </div>
    <br>
@endforeach

The above loop will result in 5 values. I want to store each of the 5 values in 5 Javascript variables to be used after that in javascript functions. How can this conversion be done in a blade?

Upvotes: 0

Views: 39

Answers (1)

Salim Djerbouh
Salim Djerbouh

Reputation: 11034

You can assign an array plucked from the collection to a Javascript object like so

<script>
    var qwinnners = {!! $qwin->pluck('id', 'title') !!};
</script>

Hope this helps

Upvotes: 1

Related Questions