skip to Main Content

I want to list down the users according to the user_types while inserting so I created the two tables and each table has connections. Each model PHP file has a relationship function. and I created the jquery code and I created the controller function but it’s not working I don’t know where I making the mistake please help me to fix this problem. I attached all code I have written and the database also.

User Type Database

enter image description here

User Database Table

enter image description here

UserType Id and User Table usty_id has connections

UserType Model

<?php

namespace Asset_Management_System;

use IlluminateDatabaseEloquentModel;

class UserType extends Model
{
    public function userpermission()
    {
        return $this->hasMany('Asset_Management_SystemUserPermission');
    }

    public function user()
    {
        return $this->hasMany('Asset_Management_SystemUser');
    }
}

User Model

class User extends Authenticatable
    {

        public function usertype()
        {
            return $this->belongsTo('Asset_Management_SystemUserType','usty_id');
        }
}

Insert Form

<div class="form-group">
                                    <label>User Type</label>
                                    <select class="form-control select2" style="width: 100%;" id="ust_id" name="ust_id">
                                        <option selected="selected">Select User Type</option>
                                        @foreach($UserType as $ust)
                                            <option value="{{$ust->id}}">{{$ust->usty_name}}</option>
                                        @endforeach
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label>User</label>
                                    <select class="form-control select2" style="width: 100%;" id="user_id" name="user_id">
                                        <option selected="selected">Select User</option>
                                        @foreach($User as $us)
                                            <option value="{{$us->id}}">{{$us->us_fname}} {{$us->us_lname}}</option>
                                        @endforeach
                                    </select>
                                </div>

Controller

public function show(Request $request)
    {
        //echo $id;
        if (!$request->usty_id) {
            $html = '<option value="">'.trans('global.pleaseSelect').'</option>';
        } else {
            $html = '';
            $user = User::where('usty_id', $request->usty_id)->get();
            foreach ($user as $us) {
                $html .= '<option value="'.$us->id.'">'.$us->us_fname.' '.$us->us_lname.'</option>';
            }
        }

        return response()->json(['html' => $html]);

    }

And Jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script type="text/javascript">
        $("#ust_id").change(function(){
            $.ajax({
                url: "{{ route('WorkRequest.show') }}?usty_id=" + $(this).val(),
                method: 'GET',
                success: function(data) {
                    $('#user_id').html(data.html);
                }
            });
        });
    </script>

Route

Route::get('WorkRequest/show', 'WorkRequestController@show')->name('WorkRequest/show');

And this is the error I’m getting when I go to the form

Missing required parameters for [Route: WorkRequest.show] [URI: WorkRequest/{WorkRequest}]. (View: C:xampphtdocsAsset_Management_Laravelresourcesviewslayoutsmain.blade.php)

Please Help me to solve this issues

2

Answers


  1. Chosen as BEST ANSWER

    This Code is working perfectly

    <script type="text/javascript">
    
                $(document).ready(function(){
    
                    // Department Change
                    $('#ust_id').change(function(){
    
                        // Department id
                        var id = $(this).val();
    
                        // Empty the dropdown
                        $('#user_id').find('option').not(':first').remove();
    
                        // AJAX request
                        $.ajax({
                            url: "{{ route('WorkRequest/show') }}?usty_id=" + id,
                            type: 'get',
                            dataType: 'json',
                            success: function(response){
    
                                //alert(response);
    
                                $('#user_id').html(response.html);
    
                            }
                        });
                    });
    
                });
    
    
            </script>
    
    public function show(Request $request)
        {
            //echo $id;
    
                $html = '';
                $user = User::where('usty_id', $request->usty_id)->get();
                foreach ($user as $us)
                {
                    $html .= '<option value="'.$us->id.'">'.$us->us_fname.' '.$us->us_lname.'</option>';
                }
    
    
            return response()->json(['html' => $html]);
    
        }
    

  2. Replace above Route with this-Route::post('workRequest/get_options','WorkRequestController@getOptions')->name('workRequest.options'); and place above the route::resource() route

    And ajax code as below

       $("#ust_id").change(function(){
            let parameter = {'usty_id': $(this).val()};
            $.ajax({
                url: "{{ route('workRequest.options') }}",
                method: 'POST',
                data: parameter,
                success: function(data) {
                    $('#user_id').html(data.html);
                }
            });
        });
    

    Add this Controller method to get options

    public function getOptions(Request $request)
        {
            if (!$request->usty_id) {
                $html = '<option value="">'.trans('global.pleaseSelect').'</option>';
            } else {
                $html = '';
                $user = User::where('usty_id', $request->usty_id)->get();
                foreach ($user as $us) {
                    $html .= '<option value="'.$us->id.'">'.$us->us_fname.' '.$us->us_lname.'</option>';
                }
            }
    
            return response()->json(['html' => $html]);
    
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search