Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

333
Views
Looping laravel array collection in jquery each

I want to append a select tag to my code using jquery and it's options need values from a laravel array collection

Controller

$array['skills'] = DB::table('skills')->get();

Script

 <script type="text/javascript">
    var skill_count=$('#skill_count').val();
    var array = {!!$array['skills']!!};

    $("#add-skill").click(function(){
        skill_count++;            
        var skill='<div class="row skill"><div class="col-sm-4"><label for="name">Skill:</label><select name="skill['+skill_count+'][skill_name]" class="form-control">$.each( array, function( i, val ) {<option value="+ val.id +"> val.skill_name </option>})</select></div><div class="col-sm-4"><label for="name">Experience:</label><select name="skill['+skill_count+'][pivot][experience_year]"   class="form-control">for(i=1;i<=30;i++){<option value="+ i +">i</option>}</select><select name="skill['+skill_count+'][pivot][experience_month]"   class="form-control">for(i=1;i<=12;i++){<option value="+i+">i</option>}</select></div><div class="col-md-12"><a class="skill-remove pull-right">- Remove Skill</a></div></div>'
        $(".show-skill").before(skill);
    });
</script>

but in browser, val.id and val.skill_name are not getting values.

console.log(val.skill_name); giving me values

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Hope this would help

var array =  [{
    id: 2,
    skill_name: 'abc'
}, {
    id: 3,
    skill_name: 'xyz'
}];

var options;
$.each( array, function( key, value ) {
console.log(value.skill_name);
  options = options + '<option value="'+value.id+'">'+value.skill_name+'</option>';
});


var skillhtml = '<div><label for="name">Skill:</label><select name="skillname">'+options+'</select></div>';

$("#container").html(skillhtml);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="container">
</div>

jsfiddle : https://jsfiddle.net/j5nkLabw/

about 4 years ago · Santiago Trujillo Report

0

Because you want echo php array, before that use json_encode

var array = {!!json_encode($array['skills'])!!}; or better encode it in controller,

$jsonArray = json_encode($array['skills']);

and after that use it in you script var array = {!! $jsonArray!!}

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!