Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

How to check if values are repeated from controller laravel

Guys iam working working with a project job portal site..

i have the education details as javascript (education.js) file.

so in my drop down the education list are displayed from that education.js file.

so a user selects the education then it is stored in the edcation_table.

Everything works fine.

But the problem is in edit the form.

when i need to edit the education detail i wll fetch the edcuation details from the db and place it in form to edit. so while placing it the value in db is also there and the values from the education.js file is also there.the values are duplicated.

Say for example if a user enter his education as 'PG' which is choosed from drop down. it enters in db. when he tries to edit that the db data 'PG' also displayed at the same time the js file PG also displayed . how to check it.

Actually in my old project what i did is the drop down details also stored in db, so that i easy checked and eliminated the duplication.

but now the education list is loaded from js file so how can i check.

this is my controller

 $education_details_edit = Education_details::find($id);
     return view('Edit-seeker-forms.edit-education-details')
        ->withEducation_details_edit($education_details_edit);


this is my edit .blade file

 <select class="form-control capitalize"id="education" name="education" required>     

                                <option style="display: {{$education_details_edit->education == "doubt" ? 'none' : ''}}" value='{{$education_details_edit->education}}'>{{$education_details_edit->education}}</option>
                            
                        </select>

Here in the place doubt i need to place a the js id so that it checks. but how i can do that.

In my old project what i did is in the place of doubt i used the variable which is picked from another table that has the drop down values.

Kindly some one help please..

this is my education.js file


var educationobject = {
   
    "Maters/PG": {
        "CA": ["CA","Pursuing","First Attempt","Second Attempt","Other"],

        "CS":["CS","Other"],       

        "MVSC":["Veterinary Science","Other"],

        "MCM":["Computers and Management","Other"],

        "MDS":["Dentistry","Other"],

        "MFA":["Sculpture","Printmaking","Visual Communication","Other"],     

    },
 
}

some one please help

0 likes
8 replies
danyal14's avatar

@abdulbazith you didn't share a code, how you save and attach the selected educations from UI to database.

But if you saving multiple selected education as JSON or serialized array in database, do the following before updating user's education.

Suppose in controller

$user_selected_educations = ["CA","Computers and Management", "Sculpture"];
$user_stored_educations = ["CA","Pursuing"];

$collection = collect($user_selected_educations, $user_stored_educations);

$collection should be unique and now you can update user education.

tinker output

php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.3-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $user_selected_educations = ["CA","Computers and Management", "Sculpture"];
=> [
     "CA",
     "Computers and Management",
     "Sculpture",
   ]
>>> $user_stored_educations = ["CA","Pursuing"];
=> [
     "CA",
     "Pursuing",
   ]
>>> $collection = collect($user_selected_educations, $user_stored_educations);
=> Illuminate\Support\Collection {#2948
     all: [
       "CA",
       "Computers and Management",
       "Sculpture",
     ],
   }
>>> 
AbdulBazith's avatar

@danyal14 thank you for your response.

Actually that's not my doubt.. everthing works fine.. a user comes, first he enters his first education details 10th std.. then it is saved in db as a row in that user id.

next the form is loaded, so he can enter his next education 12 th std so second row. like this it moves on..

to be clear whats my doubt is, when the inserted data comes to edit, the drop down values is displayed twice

so how to rectify it..

if the dropdown down also from db then we can write a simple check condition which i mentioned above.

but ny drop down is fetched from js file so how can i check it,,

Snapey's avatar
Snapey
Best Answer
Level 122

I cant easily write any code here as I am mobile, but the process is basically;

foreach over the options in your javascript file

at each option, check if the user education contains the current option

if it does, then mark that option as selected.

AbdulBazith's avatar

@snapey thank you for you reply...

s good idea.. i didnt think that..

Let me try that..

AbdulBazith's avatar

@snapey very sorry for these much days delay.

Just now i tried , first i cant alert the variable returned from controller

just i tried this

alert({{$education_details_edit->education}}); even this shows error.

i unable to predict where to check it.

this is my js

var educationobject = {

    "SSLC" : {

        "CBSE": ["English Medium", "Tamil Medium", "Other"],

        "CISCE(ICSE/ISC)": ["English Medium", "Tamil Medium", "Other"],

        "Matriculation": ["English Medium", "Tamil Medium", "Other"],

        "State Board": ["English Medium", "Tamil Medium", "Other"],

        "Other": ["Other"],
    },


"Other" : {

        "Other": ["Other"],
    },


actually i have three drop downs.

1st drop down posess SSLC and Other

if SSLC is selected then next dropdown changes to

CBSE,CISCE(ICSE/ISC),Matriculation,State Board these options will be displayed

then if CBSE is selected then the third dropdown changes English Medium, Tamil Medium, Other

this is the loop for that..


window.onload = function () {
    var education = document.getElementById("education"),
        course_or_board = document.getElementById("course_or_board"),
        specialization_or_medium = document.getElementById("specialization_or_medium");
    for (var edu in educationobject) {
        education.options[education.options.length] = new Option(edu, edu);
    }
    education.onchange = function () {
        course_or_board.length = 1; // remove all options bar first
        specialization_or_medium.length = 1; // remove all options bar first
        if (this.selectedIndex < 1) return; // done
        for (var course in educationobject[this.value]) {
            course_or_board.options[course_or_board.options.length] = new Option(course, course);
        }
    }
    education.onchange(); // reset in case page is reloaded
    course_or_board.onchange = function () {
        specialization_or_medium.length = 1; // remove all options bar first
        if (this.selectedIndex < 1) return; // done
        var special = educationobject[education.value][this.value];
        for (var i = 0; i < special.length; i++) {
            specialization_or_medium.options[specialization_or_medium.options.length] = new Option(special[i], special[i]);
        }
    }
}



education = my first dropdown name

course_or_board= my second dropdown name

specialization_or_medium= my third drop down name

here where can i check the condition which is returned from the controller.

Kindly help mee...

and the below mentioned are data returned from controller. which i need to check with the three drop downs.


{{$education_details_edit->education}}  = my controller return which contains education column data

{{$education_details_edit->course_or_board}} =  my controller return which contains course_or_board column data

{{$education_details_edit->specialization_or_medium}} =  my controller return which contains specialization_or_medium column data

1 like

Please or to participate in this conversation.