How to use laravel array into code for loop(foreach)How do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?How do I loop through or enumerate a JavaScript object?How do you check if a variable is an array in JavaScript?How to loop through a plain JavaScript object with the objects as members?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?

Cropping a message using array splits

Should these notes be played as a chord or one after another?

Delta TSA-Precheck status removed

International Code of Ethics for order of co-authors in research papers

Is there a need for better software for writers?

semanage not changing file context

Drawing Quarter-Circle

How does Howard Stark know this?

What does this quote in Small Gods refer to?

Is there enough time to Planar Bind a creature conjured by a one hour duration spell?

LocalDate.plus Incorrect Answer

Make all the squares explode

Looking for a simple way to manipulate one column of a matrix

Renting a house to a graduate student in my department

On studying Computer Science vs. Software Engineering to become a proficient coder

What does i386 mean on macOS Mojave?

Is there a faster way to calculate Abs[z]^2 numerically?

What's the best way to update Homebrew when upgrading macOS?

Why does "decimal.TryParse()" always return 0 for the input string "-1" in the below code?

How did Thanos not realise this had happened at the end of Endgame?

Can I do brevets (long distance rides) on my hybrid bike? If yes, how to start?

Is the schwa sound consistent?

Is "now" UTC time in Solidity?

Are there variations of the regular runtimes of the Big-O-Notation?



How to use laravel array into code for loop(foreach)


How do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?How do I loop through or enumerate a JavaScript object?How do you check if a variable is an array in JavaScript?How to loop through a plain JavaScript object with the objects as members?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I need to use laravel variables (foreach($attributes as $attribute)) into javascript code



I have dynamic input add or remove the page in laravel blade. visit: Dynamically Add or Remove input fields using JQuery



Now I want to use select and dropdown instead of simple input (name) but how to pass variables collection into script tag?



This is first static dropdown:



<table class="table table-bordered" id="dynamic_field">
<tr>
<td>
<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
<option value="value">name</option>
<option value="value2">name2</option>
<option value="value3">name3</option>
</select>
</td>
<td>
<input type="text" name="attribute[1]['price']" placeholder="Enter your Price" class="form-control name_list" />
</td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>


and this is my code in script tag:



$('#add').click(function()
i++;
$('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="attribute['+i+']['name']" placeholder="Enter your Name" class="form-control name_list" /></td><td><input type="text" name="attribute['+i+']['price']" placeholder="Enter your Price" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
);


I want to create something like this instead of attribute[]['name'] in script tag:



<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
@foreach($attributes as $attribute)
<option value="$attribute->id">$attribute->name</option>
@endforeach
</select>









share|improve this question
























  • im unsure what your question is. The last code sample is something that should work. If you put your <script> tag inside a laravel viewname.blade.php file you can even load php code inside your <script> tags.

    – Flame
    Mar 23 at 14:40











  • @Flame I want to use laravel variable in $('#dynamic_field').append

    – mohammad Hosseini
    Mar 23 at 16:24











  • if you read my comment that is what I said you could do. You can use $myVar in your javascript as long as it is in the blade.php file

    – Flame
    Mar 23 at 16:39

















0















I need to use laravel variables (foreach($attributes as $attribute)) into javascript code



I have dynamic input add or remove the page in laravel blade. visit: Dynamically Add or Remove input fields using JQuery



Now I want to use select and dropdown instead of simple input (name) but how to pass variables collection into script tag?



This is first static dropdown:



<table class="table table-bordered" id="dynamic_field">
<tr>
<td>
<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
<option value="value">name</option>
<option value="value2">name2</option>
<option value="value3">name3</option>
</select>
</td>
<td>
<input type="text" name="attribute[1]['price']" placeholder="Enter your Price" class="form-control name_list" />
</td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>


and this is my code in script tag:



$('#add').click(function()
i++;
$('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="attribute['+i+']['name']" placeholder="Enter your Name" class="form-control name_list" /></td><td><input type="text" name="attribute['+i+']['price']" placeholder="Enter your Price" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
);


I want to create something like this instead of attribute[]['name'] in script tag:



<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
@foreach($attributes as $attribute)
<option value="$attribute->id">$attribute->name</option>
@endforeach
</select>









share|improve this question
























  • im unsure what your question is. The last code sample is something that should work. If you put your <script> tag inside a laravel viewname.blade.php file you can even load php code inside your <script> tags.

    – Flame
    Mar 23 at 14:40











  • @Flame I want to use laravel variable in $('#dynamic_field').append

    – mohammad Hosseini
    Mar 23 at 16:24











  • if you read my comment that is what I said you could do. You can use $myVar in your javascript as long as it is in the blade.php file

    – Flame
    Mar 23 at 16:39













0












0








0








I need to use laravel variables (foreach($attributes as $attribute)) into javascript code



I have dynamic input add or remove the page in laravel blade. visit: Dynamically Add or Remove input fields using JQuery



Now I want to use select and dropdown instead of simple input (name) but how to pass variables collection into script tag?



This is first static dropdown:



<table class="table table-bordered" id="dynamic_field">
<tr>
<td>
<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
<option value="value">name</option>
<option value="value2">name2</option>
<option value="value3">name3</option>
</select>
</td>
<td>
<input type="text" name="attribute[1]['price']" placeholder="Enter your Price" class="form-control name_list" />
</td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>


and this is my code in script tag:



$('#add').click(function()
i++;
$('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="attribute['+i+']['name']" placeholder="Enter your Name" class="form-control name_list" /></td><td><input type="text" name="attribute['+i+']['price']" placeholder="Enter your Price" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
);


I want to create something like this instead of attribute[]['name'] in script tag:



<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
@foreach($attributes as $attribute)
<option value="$attribute->id">$attribute->name</option>
@endforeach
</select>









share|improve this question
















I need to use laravel variables (foreach($attributes as $attribute)) into javascript code



I have dynamic input add or remove the page in laravel blade. visit: Dynamically Add or Remove input fields using JQuery



Now I want to use select and dropdown instead of simple input (name) but how to pass variables collection into script tag?



This is first static dropdown:



<table class="table table-bordered" id="dynamic_field">
<tr>
<td>
<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
<option value="value">name</option>
<option value="value2">name2</option>
<option value="value3">name3</option>
</select>
</td>
<td>
<input type="text" name="attribute[1]['price']" placeholder="Enter your Price" class="form-control name_list" />
</td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>


and this is my code in script tag:



$('#add').click(function()
i++;
$('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="attribute['+i+']['name']" placeholder="Enter your Name" class="form-control name_list" /></td><td><input type="text" name="attribute['+i+']['price']" placeholder="Enter your Price" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
);


I want to create something like this instead of attribute[]['name'] in script tag:



<select class="form-control m-select2" id="m_select2_1" name="attribute[1]['name']">
@foreach($attributes as $attribute)
<option value="$attribute->id">$attribute->name</option>
@endforeach
</select>






javascript laravel laravel-5 laravel-blade






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 10:55









Udhav Sarvaiya

2,71392131




2,71392131










asked Mar 23 at 10:53









mohammad Hosseinimohammad Hosseini

3968




3968












  • im unsure what your question is. The last code sample is something that should work. If you put your <script> tag inside a laravel viewname.blade.php file you can even load php code inside your <script> tags.

    – Flame
    Mar 23 at 14:40











  • @Flame I want to use laravel variable in $('#dynamic_field').append

    – mohammad Hosseini
    Mar 23 at 16:24











  • if you read my comment that is what I said you could do. You can use $myVar in your javascript as long as it is in the blade.php file

    – Flame
    Mar 23 at 16:39

















  • im unsure what your question is. The last code sample is something that should work. If you put your <script> tag inside a laravel viewname.blade.php file you can even load php code inside your <script> tags.

    – Flame
    Mar 23 at 14:40











  • @Flame I want to use laravel variable in $('#dynamic_field').append

    – mohammad Hosseini
    Mar 23 at 16:24











  • if you read my comment that is what I said you could do. You can use $myVar in your javascript as long as it is in the blade.php file

    – Flame
    Mar 23 at 16:39
















im unsure what your question is. The last code sample is something that should work. If you put your <script> tag inside a laravel viewname.blade.php file you can even load php code inside your <script> tags.

– Flame
Mar 23 at 14:40





im unsure what your question is. The last code sample is something that should work. If you put your <script> tag inside a laravel viewname.blade.php file you can even load php code inside your <script> tags.

– Flame
Mar 23 at 14:40













@Flame I want to use laravel variable in $('#dynamic_field').append

– mohammad Hosseini
Mar 23 at 16:24





@Flame I want to use laravel variable in $('#dynamic_field').append

– mohammad Hosseini
Mar 23 at 16:24













if you read my comment that is what I said you could do. You can use $myVar in your javascript as long as it is in the blade.php file

– Flame
Mar 23 at 16:39





if you read my comment that is what I said you could do. You can use $myVar in your javascript as long as it is in the blade.php file

– Flame
Mar 23 at 16:39












1 Answer
1






active

oldest

votes


















0














You can use @json or !! json_encode() !! to assign it to a javascript variable in your .blade.php file.



<script>
let data = @json($attributes)
</script>


In JS file, data variable will have the array you want:



console.log(data);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312952%2fhow-to-use-laravel-array-into-script-code-for-loopforeach%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use @json or !! json_encode() !! to assign it to a javascript variable in your .blade.php file.



    <script>
    let data = @json($attributes)
    </script>


    In JS file, data variable will have the array you want:



    console.log(data);





    share|improve this answer



























      0














      You can use @json or !! json_encode() !! to assign it to a javascript variable in your .blade.php file.



      <script>
      let data = @json($attributes)
      </script>


      In JS file, data variable will have the array you want:



      console.log(data);





      share|improve this answer

























        0












        0








        0







        You can use @json or !! json_encode() !! to assign it to a javascript variable in your .blade.php file.



        <script>
        let data = @json($attributes)
        </script>


        In JS file, data variable will have the array you want:



        console.log(data);





        share|improve this answer













        You can use @json or !! json_encode() !! to assign it to a javascript variable in your .blade.php file.



        <script>
        let data = @json($attributes)
        </script>


        In JS file, data variable will have the array you want:



        console.log(data);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 21:42









        sentysenty

        4,040657141




        4,040657141





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312952%2fhow-to-use-laravel-array-into-script-code-for-loopforeach%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript