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

Nakov's avatar

@kanchan186 so that's whats wrong.. the designation id is missing. In your designation model, do you override the primary key:

protected $primaryKey = 'designation_id';

?

Try this instead then:

Designation::where('department_id', 10)->get(['designation_id', 'designation_name'])

Then your JS code:

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

after updating code

getting error

Illuminate \ Database \ QueryException (22007)
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'undefined' for column `db_partsanalysis`.`employees`.`designation_id` at row 1

in designation drop-down shows undefined in place of values

Nakov's avatar

@kanchan186 use the above steps to debug please.. don't wait for me line by line:

console.log(data);

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

What is the result on this..

Kanchan186's avatar

@nakov using this code

error

employee:1743 Uncaught ReferenceError: getDesignation is not defined
    at HTMLSelectElement.onchange (employee:1743)
Nakov's avatar

@kanchan186 did you removed the function completely? Revert your changes and replace just the $.each usage in your code.

Kanchan186's avatar

now console gives output

when selected purchase means department_id=2

Nakov's avatar

@kanchan186 can you share your Designation model code please?

Do you have protected $primaryKey = 'designation_id'; in it?

Kanchan186's avatar

Designation model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Designation extends Model
{
    protected $fillable =['designation_name','department_id'];
   protected $primaryKey = 'designation_id';
}
Nakov's avatar

@kanchan186 I don't understand what are you doing so that the designation ID does not comes back to the view at all.. What is the result in your console or browser if you return this

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

console returns

browser returns

using dd($request->designation_id);

output= "undefined"

Nakov's avatar

@kanchan186 so the problem is that you designation_id is not returned at all from the query that you use, and I have no idea why, and what are you doing. $request->designation_id is step ahead, you cannot get a value there, unless you fix your designationAjax method to return the correct values..

what is the output of this: http://localhost/partsanalysis/public/designation/ajax/1

and then what is the output of console.log(data); before the $.each function.

Please learn to troubleshoot, it takes forever to tell you each line to try out.

Kanchan186's avatar

@nakov please help!!

i also not found the reason why designation_id taking 0

Nakov's avatar

@kanchan186 so how else can I help you? Did you checked why the designation_id does not exists in the results above?

If you found why it is 0, have you changed that to use the correct values?

fylzero's avatar

@nakov You should really consider billing the man at this point...

@kanchan186 If you want further help, please email Nakov your credit card number. =)

(...I'm clearly joking)

1 like
Nakov's avatar

@fylzero yeah, good point :D if you see all of his threads, are over 30 replies :) trying to help, but he just won't consider learning at all, or doing the steps that are already answered 100 times.

1 like
Previous

Please or to participate in this conversation.