zaster's avatar

Dependent Menu - Jquery - JSON

I am getting this error in my console when selecting a customer from the dropdown

GET http://dependentmenu.test/json-companies/2 404 (Not Found)

web.php

Route::get('', 'CustomerController@customers');

Route::get('json-companies/{$id}','CustomerController@companies');

CustomerController.php

<?php

namespace App\Http\Controllers;
use App\Customer;
use App\Company;

use Illuminate\Http\Request;

class CustomerController extends Controller
{
    
    public function customers()
    {
        $customers = Customer::all();
         
        return view('customers', compact('customers'));
    }


    public function companies($id)
    {        
        $customer_id = $id;        
        $companies= Company::where('customer_id', '=', $customer_id)->get();
      
        return response()->json($companies);
    }
    
}

customers.blade.php

<!doctype html>
<html lang="en">
  <head>
    <title>Title</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    {{-- Jquery Data Table css--}}
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">

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

    {{-- Jquery Data Table js--}}
    <script charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-12">
                <div class="form-group">
                  <label for="customer">Customer</label>
                  <select class="form-control" name="customer" id="customer">
                    @foreach ($customers as $customer)
                        <option value="{{$customer->id}}">{{ $customer->customer }}</option>
                    @endforeach
                  </select>
                </div> 
                <div class="form-group">
                  <label for="">Your Companies</label>
                  <select class="form-control" name="companies" id="company">
                    <option value="0" disable="true" selected="true">=== Select Companies ===</option>
                  </select>
                </div>
            </div>

        </div>
    </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    
  </body>

    <script>
      $(document).ready(function() {
          $("#customer").on('change', function() {
              var customerID = $(this).val();
              if(customerID) {
                  $.ajax({
                      url: '/json-companies/'+customerID,
                      type: "GET",
                      dataType: "json",
                      success:function(data) {
                          $("company").empty();
                          $.each(data, function(key, value) {
                              $("company").append('<option value="'+ value.id +'">'+ value.name +'</option>');
                          });
                      }
                  });
              }else{
                  $("company").empty();
              }
          });
      });
</script>
</html>
0 likes
5 replies
Cronix's avatar
Cronix
Best Answer
Level 67

Remove the $ from $id in your route. It should just be the placeholder {id}.

Route::get('json-companies/{id}','CustomerController@companies');

This doesn't have to do with the problem, but you're also missing an id or class (#/.) in several places, like

$("company").empty();
$("company").append(//...

should be .company or #company?

zaster's avatar

@cronix

corrected the Route

and

corrected #company

I am very happy that it made progress. Now the issue is that i am getting

undefined values in the company dropdown

Example : When customer 1 is selected , there are two undefined entries in the dropdown, instead of that it should display company 1 and company 2

zaster's avatar

@cronix

ok figured it out. it was the

value.name part. corrected it(Something to do with the companies table column name)

Thank you very much @cronix

zaster's avatar

@cronix

CustomerController.php

This is an additional question. What would you suggest if the relationship between Customer and Company is a Many to Many

what changes should i do? Especially in the

public function companies($id){
$customer_id = $id;

// What would be the query ?

return response()->json($companies);

}

zaster's avatar

@cronix

public function companies($id)
{        
    $customer = Customer::find($id);
    $companies = $customer->companies()->get();
    return response()->json($companies);
}

worked for me :)

Please or to participate in this conversation.