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

melx's avatar
Level 4

get data based on select dropdown list

i have this two tables

customer customer table

           protected $fillable= 
               ['compy_name','phone','address_name','master_price','customer_type','slave_price','currency'];

customer order table.

           protected $fillable=[  
                                   'orderNumb',
                                  'customer_id',
                                  'loading_point',
                                   'looade_date',
                                    'border_id',
                                    'team_id',
                                    'truck_number',
                                     'trailer_number',
                                        'master_qty',
                                      'slave_qty',
                                       'user_id'  ];

so i want when i select the compy_name, i get their truck_number as well.

i tried like this but does not works

                  Route::get('customers/{customer}/customer__orders', function (App\Customer_Orders $customer) 
              {
               return response()->json($customer->customer_order->map(function ($customer_order) {
                 return ['id' => $customer_order->id, 'text' => $customer_order->truck_number];
           })
        );
     });

in my blade

             $(document).ready(function(){
      $("#customer_id").change(function () {
          var options = [];
          $.ajax({
            method: 'GET',
            url: '/customers/' + this.value + '/customer__orders',
            success: function (options) {
                window.options = options
                $('#truck_id').empty()
                $('#truck_id').select2({data: options});
            }
          })
      })
      })

correct me where i did wrong

0 likes
5 replies
vincent15000's avatar

Hello,

Do you declare the relationships in your models ?

V

Snapey's avatar

truck number is attribute of customer order

presume customer has many orders

you cannot get truck number for customer because it could be different with every order

melx's avatar
Level 4

@snapey,

What is the alternative to get those trucks number?

Or May be can i select by ordernumber?

Or when i select customer i get the order list in another dropdownlist, then if i select that specific order number i get his truck number?

Or what is your advice?

Snapey's avatar

If there is more than one order then you need to find out which order they are interested in.

I cant advise because I don't know the context

Please or to participate in this conversation.