javascript – Retrieve the id of a select with laravel and ajax-ThrowExceptions

Exception or error:

I have two tables: role and persons.

In my persons table I have the foreign key ‘idrole’ of the table: role. I created a drop-down list which retrieves the roles from my table: role

My problem is as follows: I can’t get the id of the selected role from the drop-down list

I am a beginner with laravel and ajax. Thank you for your help

Code of my html page :

<span>Name Role: </span>
<select style="width: 200px" class="namerole" id="nom">

    <option value="0" disabled="true" selected="true">-Select-</option>
    @foreach($roles as $role)
        <option value="{{$role->nom}}">{{$role->nom}}</option>


<span>Id Role: </span>
<input type="text" class="idrole" id="idrole" disabled>

<script src=""></script>

<script type="text/javascript">
    $(document).ready(function() {

        $(document).on('change', '.namerole', function () {

            var role_nom = $(this).val();

            var div = $(this).parent();

                type: 'get',
                url: '{!!URL::to('findIdRole')!!}',
                data: {'nom':role_nom},
                success: function (data) {


My function:

public function findIdRole(Request $request){
    return response()->json($rj);
How to solve:

use$ request->get(‘nom’) instead of $request->nom ,

i am not sure but probably it is coming as array and also not an object

public function findIdRole(Request $request){
        ->where('nom',$request->get('nom')) // here is the change
    return response()->json($rj);

Leave a Reply

Your email address will not be published. Required fields are marked *