Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get 2 table data into single variable array in laravel?

i have 2 tables User and subadmin consider user have 3 columns and subadmin has 2 column i want to get 3+2 (5 column) data into a single veriable array

the technique i want to use is that in user table i have id which is same in subadmin table with sub_admin_id(column) how i can use eloquent model to first link id with sub_admin_id and then into a single query get 5 column in single veriable array

here i am using to get data

$subadmindata = User::find($id)->get(); // [column1 ,column2 ,column3 ]
$subadmindata1 = SubAdmin::find($id)->get(); // [column1 ,column2 ]

output should be

$data = // [column1 ,column2 ,column3 , column4 ,column5 ]

note i dont want to use array merge or combine method i want to use eloquent model for my learning

like image 989
Hamza Qureshi Avatar asked Nov 02 '25 17:11

Hamza Qureshi


2 Answers

you could use concat like this

$subadmindata = User::find($id)->get();
$subadmindata1 = SubAdmin::find($id)->get(); // it will return array of collections

$data = $subadmindata->concat($subadmindata1);

Notice when you use get after find it stop it's jobs so there is no need to find here

like image 138
Joseph Avatar answered Nov 04 '25 10:11

Joseph


get() method will give you a collection not array, so you can merge two collection as follows.

   $subadmindata = User::find($id)->get();
   $subadmindata1 = SubAdmin::find($id)->get();

    $data = $subadmindata->merge($subadmindata1);
like image 39
Prashant Deshmukh..... Avatar answered Nov 04 '25 10:11

Prashant Deshmukh.....