Get dealership under a company

I have branch index page it contains 2 drop-down menu called company and dealership when i click on company it contains a company i created when click on a company the corresponding dealership should list in the dealership dropdown. i used eloqent straightly into index page i did that because the i can't access the company and dealership in the index page

    Index

 @include('theme.header')

    <?php  use Illuminate\Support\Facades\DB;?>


    <div class="page-content-wrapper ">

        <div class="container-fluid">

            <div class="row">
                <div class="col-sm-12">
                    <div class="page-title-box">
                        <div class="btn-group float-right">

                        </div>
                        <h4 class="page-title">Branch Management</h4>
                    </div>
                </div>
            </div>
            <!-- end page title end breadcrumb -->

            <div class="row">
                <div class="col-12">
                    <div class="card m-b-30">
                        <div class="card-body">

                            <h4 class="mt-0 header-title">Branch</h4>
                            <br>
                            <br>
                            <form id="form" method="post" action="{{route('branch.store')}}">
                                {{csrf_field()}}
                                <div class="form-group row">
                                    <label class="col-sm-2 col-form-label">Company</label>
                                    <div class="col-sm-10">

                                        <select class="form-control" id="company" name="company">

                                            <option>Select Company</option>
                                            @foreach(\App\Company::all()->where('status','0') as $company)
                                                <option value="{{$company->comp_id}}">{{$company->name}}</option>
                                            @endforeach

                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-sm-2 col-form-label">Dealership</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" id="dealer" name=" dealer">
                                            <option>Select Dealership</option>
                                            @foreach(\App\Dealership::join('companies','comp_id','=','dealerships.comp_id')->where('status','0') as $dealership)

                                                <option value="{{$dealership->dlr_id}}">{{$dealership->name}}</option>
                                            @endforeach

                                        </select>
                                    </div>
                                </div>

                                <div class="form-group row">
                                    <label for="example-text-input" class="col-sm-2 col-form-label">Email</label>
                                    <div class="col-sm-10">
                                        <input class="form-control" type="email" id="email" name="email" required>
                                    </div>
                                </div>

                                <div class="form-group row">
                                    <label for="example-text-input" class="col-sm-2 col-form-label">Branch Name</label>
                                    <div class="col-sm-10">
                                        <input class="form-control" type="text" id="branch" name="branch" required>
                                    </div>
                                </div>


                                <div class="row">
                                    <div class="col-sm-12">
                                        <div class="page-title-box">
                                            <div class="btn-group float-right">
                                                <button class="btn btn-primary" id="btn_save" data-toggle="modal"
                                                        data-target="#create" type="submit">Save
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </form>


                        </div>
                    </div>


     @include('theme.footer')

4 answers

  • answered 2018-04-17 05:14 Sakibur Rahman

    If you want to dynamically change the contents of dealership dropdown, on the event of changing the company dropdown, you should use javascript, jquery or similar javascript framework, because PHP is a server side scripting language and require page refresh to change the contents of a web page.

  • answered 2018-04-17 05:14 Muhammad Nauman

    First thing first. You need to change queries.

    Company::where(‘status’, 0)->get();
    

    And next you missed the tailing get() in the next dropdown. And why are you not using relationships to query?

  • answered 2018-04-17 05:14 Ma Kobi

    Like Muhammad Naumann already said you have to use the get() method to get the actual data:

    Company::where(‘status’, 0)->get();
    

    On the company select field you could add an event listener like onChange. In this listener you run a ajax get request to fetch the dealerships for the selected company.

  • answered 2018-04-17 05:14 Abhijith

    I figure it out with ajax.

    1.Create a custom function in controller file.in this case Branch controller, this function contains query builder to retrieve the data

    2.Write ajax, in the index file, the dealership dropdown is hidden default when you select the company the dropdown will show corresponding data.

    Index File
    ----------
        @include('theme.header')
    
        <?php  use Illuminate\Support\Facades\DB;?>
        <script language="javascript">
    
    
            /*--- Fliter dealership corressponging company---*/
    
            $(document).ready(function () {
                $('#dealership_div').hide();
    
                $('#company').change(function () {
                    alert('hello');
                    $('#dealership_div').show();
                    let id = this.value;
                    $.ajax({
    
                        url: '/filter_dealer',
                        type: "post",
                        data: {option: id},
                        success: function (data) {
                            $('#dealer')
                                .find('option')
                                .remove()
                                .end()
                                .append(" <option value=''>--- Select dealership ---</option>")
    
                            $.each(data, function (key, value) {
                                $('#dealer')
                                    .append($("<option></option>")
                                        .attr('value', value['dlr_id'])
                                        .text(value['name'])
                                    );
                            });
    
    
                        },
                        error: function () {
                            alert("Error occurred While Processing");
                        }
    
    
                    });
                });
            });
    
    
        </script>
    
        <div class="page-content-wrapper ">
    
            <div class="container-fluid">
    
                <div class="row">
                    <div class="col-sm-12">
                        <div class="page-title-box">
                            <div class="btn-group float-right">
    
                            </div>
                            <h4 class="page-title">Branch Management</h4>
                        </div>
                    </div>
                </div>
                <!-- end page title end breadcrumb -->
    
                <div class="row">
                    <div class="col-12">
                        <div class="card m-b-30">
                            <div class="card-body">
    
                                <h4 class="mt-0 header-title">Branch</h4>
                                <br>
                                <br>
                                <form id="form" method="post" action="{{route('branch.store')}}">
                                    {{csrf_field()}}
                                    <div class="form-group row">
                                        <label class="col-sm-2 col-form-label">Company</label>
                                        <div class="col-sm-10">
    
                                            <select class="form-control" id="company" name="company">
    
                                                <option>Select Company</option>
                                                @foreach(\App\Company::all()->where('status','0') as $company)
                                                    <option value="{{$company->comp_id}}">{{$company->name}}</option>
                                                @endforeach
    
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group row" id="dealership_div">
                                        <label class="col-sm-2 col-form-label">Dealership</label>
                                        <div class="col-sm-10">
                                            <select class="form-control" id="dealer" name=" dealer">
    
                                                <option></option>
    
                                            </select>
                                        </div>
                                    </div>
    
                                    <div class="form-group row">
                                        <label for="example-text-input" class="col-sm-2 col-form-label">Email</label>
                                        <div class="col-sm-10">
                                            <input class="form-control" type="email" id="email" name="email" required>
                                        </div>
                                    </div>
    
                                    <div class="form-group row">
                                        <label for="example-text-input" class="col-sm-2 col-form-label">Branch Name</label>
                                        <div class="col-sm-10">
                                            <input class="form-control" type="text" id="branch" name="branch" required>
                                        </div>
                                    </div>
    
    
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <div class="page-title-box">
                                                <div class="btn-group float-right">
                                                    <button class="btn btn-primary" id="btn_save" data-toggle="modal"
                                                            data-target="#create" type="submit">Save
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
    
                                </form>
    
    
                            </div>
                        </div>
    

    Branch Controller File With a custom function

     public function filter_dealer(Request $request)
        {
            $company_id=$request->input('option');
            $dealership=DB::table('dealerships')->select('dlr_id','name')->where([['comp_id','=',$company_id],['status','=','0']])->get();
            return response()->json($dealership);
    
        }
    

    Route File

    Route::post('filter_dealer', 'BranchController@filter_dealer')->name('filter_dealer');