Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

model view controller - 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

question from:https://stackoverflow.com/questions/65844946/how-to-get-2-table-data-into-single-variable-array-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...