2

I have this code

$datas = DB::table('uni_staff')
->join('department', 'uni_staff.department_id', 'department.id')
->join('uni_staff_type', 'uni_staff.type_id', 'uni_staff_type.id')
->select('uni_staff.*', 'department.dep_name as departmentName' , 'uni_staff_type.user_type as 
  typeName')
->where('uni_staff.id', $id)
->select('uni_staff.*')
->get();

I want to put every column value of a row to an indexed array, or print the result by column index like or something else $datas[0] not column names like $datas->id.

2
  • Can you elaborate a bit more on what the intended outcome and usage is? Commented Nov 23, 2020 at 10:54
  • @MhluziBhaka this is my result of query : {"id":3,"first_name":"pepe","middle_name":"ahmad","last_name":"ali","full_name":"pep shan kj","pho_number":"ml;k","user_code":"ljl;j","type_id":1,"department_id":2,"internet1":"5654","internet2":"545","printer_code":546,"status":"544","date":"2020-11-18","note":"dsds","add_by":"dsd","departmentName":"Design","typeName":"Employee"} I want to put all column values to an array then use loop to print the array Commented Nov 24, 2020 at 6:03

2 Answers 2

2

Without having to change any configuration you could use the Collection to map those objects to arrays and then call array_values on them:

$datas->map(fn ($row) => array_values((array) $row));

Laravel 8.x Docs - Collections - Available Methods - map

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried using toArray?

$datas=DB::table('uni_staff')
->join('department','uni_staff.department_id','department.id')
->join('uni_staff_type','uni_staff.type_id','uni_staff_type.id')
->select('uni_staff.*','department.dep_name as departmentName' , 
'uni_staff_type.user_type as typeName')
->where('uni_staff.id',$id)
->select('uni_staff.*')
->get()
->toArray();

2 Comments

they would end up with an array of stdClass objects
Error message : " Cannot use object of type stdClass as array "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.