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

Kanchan186's avatar

The selected designation id is invalid.

please check my code where i going wrong

Controller

public function store(Request $request)
    {
        //dd(request()->all());
         $this->validate($request,[
                                'emp_fname'=>'unique:employees|required',
                                'designation_id' => 'exists:designations,department_id' // checks if the is a record matching the provided value
                                ]);

           if($request->hasFile('emp_photo')){
            

            $file = $request->file('emp_photo');

            $destinationPath = public_path().'/emp_photos/';
            $filename = $file->getClientOriginalName();
            if(!File::exists($destinationPath))
             {
                File::makeDirectory($destinationPath, $mode = 0777, true);
                $file->move($destinationPath, $filename);
             }
             else
             {
                $file->move($destinationPath, $filename);
             }
            

         //   echo  $filename;exit;
            }//end of if

        $emp_id=employee::create([
        //'emp_id' => request('emp_id'),
        'emp_photo' =>$filename,    
        'emp_fname' => request('emp_fname'),
        'emp_lname' => request('emp_lname'),
        'emp_email' => request('emp_email'),
        'emp_bdate' => request('emp_bdate'),
        'emp_mobile' => request('emp_mobile'),
        'emp_alt_mobile' => request('emp_alt_mobile'),
        'emp_country' => request('emp_country'),
        'emp_state' => request('emp_state'),
        'emp_city' => request('emp_city'),
        'emp_address' => request('emp_address'),
        'emp_pincode' => request('emp_pincode'),
        'gender' => request('gender'),
        'department_id' => request('department_id'),
        'designation_id' => request('designation_id'),
        'emp_id_proof' => request('emp_id_proof'),
        'emp_username' => request('emp_username'),
        'password' => request('password'),
        'login_first'=>request('login_first'),
        'report_to_id' => request('report_to_id'),
        'login_first' => 0,

        ]);
        

        
        return redirect('employee/view');
    }

    public function show()
    {
        $employee=employee::join('departments','employees.department_id','=','departments.department_id')
         ->join('designations','employees.designation_id','=','designations.designation_id')
        ->get();

        $department=Department::get();
        $designation=designation::get();
        
        //return $names;

        return view('backend.employee.viewEmp',compact( 'department','employee'));
    }

    public function designationAjax($department)
    {
        $designation = Designation::where("department_id",$department)
                    ->pluck('designations.designation_name','designations.designation_id')->all();
        return json_encode($designation);
    }

view

             <div class="form-group">
                                        <h5>Select Department <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control" name="department_id" onchange="getDesignation(this.value)" required>
                                                <option>--Select Department--</option>
                                            @foreach($department as $br)
                                            <option value="{{$br->department_id}}">{{$br->department_name}}</option>
                                            @endforeach
                                        </select>

                                        </div>
                                       
                                    </div>

                                    <div class="form-group">
                                        <h5>Select Designation <span class="text-danger">*</span></h5>
                                        <div class="controls">
                                            
                                             <select class="form-control " name="designation_id" required>
                                                <option>--Select Designation--</option>
                                           
                                        </select>


                                         

                                        </div>
                                       
                                    </div>

script

 function getDesignation(department)
                            {
                               if(department) {
                                        $.ajax({
                                            url: '{{url('/')}}/designation/ajax/'+department,
                                            type: "GET",
                                            dataType: "json",
                                            success:function(data) {
                                        
                                                $('select[name="designation_id"]').empty();
                                                $('select[name="designation_id"]').prepend('<option value="">--Select designation--</option>');
                                                $.each(data, function(key, value) {
                                                    $('select[name="designation_id"]').append('<option value="'+ key +'">'+ value +'</option>');
                                                });

                                            }
                                        });
                                    } else{
                                         $('select[name="designation_id"]').empty();
                                    }
                            }
0 likes
46 replies
tykus's avatar

Your validation rule is checkin the department_id column on the designations table; it shoujld be checking the designation_id if that is the primary key.

  $this->validate($request,[
    'emp_fname'=>'unique:employees|required',
    'designation_id' => 'exists:designations,designation_id'
  ]);
Kanchan186's avatar

@tykus still it gets same error

The selected designation id is invalid.

is there any problem in my logic in controller store method. please check.

fylzero's avatar

@kanchan186 Try to dd($request->designations_id) before validation... see what you get, check the table and make sure it exists manually.

1 like
Kanchan186's avatar

dd($request->designations_id)

it gets output

null

fylzero's avatar

@kanchan186 Sounds like the I'd value isn't being passed in at all.

What is your code that posts to the route that hits your store method in your controller?

1 like
Nakov's avatar

@fylzero but he checked exactly what you gave him, and there is a typo, it is not designations_id but designation_id instead.

fylzero's avatar

@kanchan186 What is your code that submits to store?

The ajax call you posted is a get... store would be a post. Where is that code?

1 like
Kanchan186's avatar

store method

 public function store(Request $request)
    {
        //dd(request()->all());

        dd($request->designation_id);
         $this->validate($request,[
                                     'emp_fname'=>'unique:employees|required',
                                     //'designation_id' => 'exists:designations,designation_id'
                                 ]);


           if($request->hasFile('emp_photo')){
            

            $file = $request->file('emp_photo');

            $destinationPath = public_path().'/emp_photos/';
            $filename = $file->getClientOriginalName();
            if(!File::exists($destinationPath))
             {
                File::makeDirectory($destinationPath, $mode = 0777, true);
                $file->move($destinationPath, $filename);
             }
             else
             {
                $file->move($destinationPath, $filename);
             }
            

         //   echo  $filename;exit;
            }//end of if

        Employee::create([
        //'emp_id' => request('emp_id'),
        'emp_photo' =>$filename,    
        'emp_fname' => request('emp_fname'),
        'emp_lname' => request('emp_lname'),
        'emp_email' => request('emp_email'),
        'emp_bdate' => request('emp_bdate'),
        'emp_mobile' => request('emp_mobile'),
        'emp_alt_mobile' => request('emp_alt_mobile'),
        'emp_country' => request('emp_country'),
        'emp_state' => request('emp_state'),
        'emp_city' => request('emp_city'),
        'emp_address' => request('emp_address'),
        'emp_pincode' => request('emp_pincode'),
        'gender' => request('gender'),
        'department_id' => request('department_id'),
        'designation_id' => request('designation_id'),
        'emp_id_proof' => request('emp_id_proof'),
        'emp_username' => request('emp_username'),
        'password' => request('password'),
        'login_first'=>request('login_first'),
        'report_to_id' => request('report_to_id'),
        'login_first' => 0,

        ]);
        

        
        return redirect('employee/view');
    }
fylzero's avatar

@kanchan186 Not what I'm looking for. What sends the data to the route that hits store? Where is your post to that route?

1 like
Kanchan186's avatar

web.php

//add employee
Route::get('employee','EmployeeController@Add');
//save employee
Route::post('employee','EmployeeController@store');
//edit employee
Route::get('employee/{employee}/edit','EmployeeController@edit');
Route::patch('employee/{employee}','EmployeeController@update');
//view employee
Route::get('employee/view','EmployeeController@show');
//delete employee
Route::get('employee/{employee}','EmployeeController@destroy');
//get country wise employee using ajax
Route::get('designation/ajax/{department}','EmployeeController@designationAjax');
fylzero's avatar

@kanchan186 Getting warmer... where is you code that posts to /employee ?

Do you have a script ajax call POSTING to /employee?

The script you put on here shows a get call.

1 like
Kanchan186's avatar
public function designationAjax($department)
    {
        $designation = Designation::where("department_id",$department)
                    ->pluck('designations.designation_name','designations.designation_id')->all();
        return json_encode($designation);
    }
Nakov's avatar
Nakov
Best Answer
Level 73

@kanchan186 I believe your problem lies here. So back to debugging.. After you get the designations from your department, try logging in the browser console if you are adding the correct values to your designation.

Share the output of this: (You can inspect it in the Browser console)

$.each(data, function(key, value) {
  
    console.log(key, value);
   
    $('select[name="designation_id"]').append('<option value="'+ key +'">'+ value +'</option>');
});

Make sure that you are selecting a different designation before you submit, and you are not submitting The "Select designation" value.

fylzero's avatar

@nakov He hasn't shown the ajax code where he is posting to the controller. Not sure how to debug this without that.

Is the blade template a form tag? Like how is this being formed before it is submitted?

1 like
Nakov's avatar

@fylzero but he is not posting using AJAX I believe. He uses ajax only to fill in the designations dropdown based on the department.

Kanchan186's avatar

@nakov

using above code in console

and after submit value of designation_id=0

Nakov's avatar

@kanchan186 so is your Product manager in the designations table with an ID of 0? That's wrong if it is so. You should make it auto increment..

Kanchan186's avatar

sir designation_id has auto increment

but on console it gives values like

Nakov's avatar

@kanchan186 then change the ajax function to this and test the result again:

public function designationAjax($department)
{
    return Designation::where("department_id",$department)->pluck('designation_name', 'designation_id')->toArray();
}
Kanchan186's avatar

@nakov using above code

in console output:

if i selected sales it gives o and 1 if i selected purchase it gives 0

Nakov's avatar

@kanchan186 something is wrong with this output here I believe:

Designation::where("department_id",$department)->pluck('designation_name', 'designation_id')->toArray();

Can you tell me above the $.each what is the output of your console.log(data);?

Nakov's avatar

@kanchan186 so as I said, the problem lies here:

Designation::where("department_id",$department)->pluck('designation_name', 'designation_id')->toArray();

You should debug why that one is not returning the correct result..

Kanchan186's avatar

@nakov as you said exactly using this code

public function designationAjax($department)
    {
        Designation::where("department_id",$department)->pluck('designation_name', 'designation_id')->toArray();
    }

still getting same output

Nakov's avatar

@kanchan186 you are not returning anything..

public function designationAjax($department)
{
        return Designation::where("department_id",$department)->pluck('designation_name', 'designation_id')->toArray();
}

So to test that in your browser try:

YOUR_URL.test/designation/ajax/1

and see what the result is. Change YOUR_URL with your development URL.

fylzero's avatar

@kanchan186 I feel like you've come full circle to what I was asking for in the beginning. What code is forming and submitting your post request? That is likely where you'll find the problem.

1 like
Nakov's avatar

@fylzero he is not getting the correct results in the first place for the dropdown, so why does it matter how he is submitting the form? Can you please tell me?

Next

Please or to participate in this conversation.