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

Kish77's avatar

Dynamically assign ID of query to anchor id attribute

I am populating a DataTable in Laravel with the results of an Inner Join query. After, that I am generating update buttons which will allow me to edit the selected record. I would like to use the id of each record returned from the Join query to be unique row id. The issue is, the query returns an array of objects so I am not sure how to access each id.

I've tried using a foreach loop, the buttons generate but they become unclickable.

Query:

     $payment = DB::table('owed_payments')
                   ->join('payments', 'sub_id' , '=', 'payments.user_id')
                   ->join('user_profiles', 'sub_id', '=', 'user_profiles.user_id')
                   ->select('owed_payments.id', 'owed_payments.sub_id', 'owed_payments.company_name',
                            'owed_payments.amount','user_profiles.first_name','user_profiles.last_name', 'owed_payments.status', 
                            'owed_payments.job_date', 'owed_payments.amount_payed', 'owed_payments.date_payed', 
                            'payments.trn_ss', 'payments.nis', 'payments.account_number', 'payments.bank_name', 'payments.bank_branch')
                    ->get();

Code to generate buttons with id:

      return DataTables::of($payment)
               ->addColumn('action', function($payment){
                   foreach($payment as $pay){
                    return '<a href="#" class="edit btn" id="'.$pay['id'].'">
                    Update</a>';
                }
               })
               ->rawColumns(['action'])

              ->make(true);

How can I achieve this?

0 likes
1 reply
Kish77's avatar

Got it guys. Just used it like $payment->id. Strange thing is, I had tried earlier and didn't get it

Please or to participate in this conversation.