3

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

Hamza Qureshi
  • 172
  • 2
  • 20

5 Answers5

1

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

Joseph
  • 5,644
  • 3
  • 18
  • 44
  • 1
    first of all thanx for every contributor but these methods i already know is there a way i use model function for this i have seen a query User::where('role', 5)->has('ordertaker')->with('ordertaker'); somthing like this i mean use model to get data into single veriable? – Hamza Qureshi Jan 22 '21 at 12:28
  • 1
    you could do that if there a relation between these two tables – Joseph Jan 22 '21 at 12:32
  • This code looks like wrong `$subadmindata.concat($subadmindata1)` – Kenath Jan 22 '21 at 12:32
  • @Kenath i test it out after adding the answer for double checking – Joseph Jan 22 '21 at 12:34
  • @Kenath not native `PHP` in `laravel` – Joseph Jan 22 '21 at 12:37
  • 1
    @Joseph The syntax looks wrong for me '.' operator you can use for concat multiple strings. You can't use it like this as far as I know. `$subadmindata->concat($subadmindata1)` may work but not `$subadmindata.concat($subadmindata1)` – Kenath Jan 22 '21 at 12:48
  • aha thanks for mention that i missed it out :) – Joseph Jan 22 '21 at 12:55
1

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);
Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11
  • when you use `get` it returns an array of collections – Joseph Jan 22 '21 at 12:20
  • first of all thanx for every contributor but these methods i already know is there a way i use model function for this i have seen a query User::where('role', 5)->has('ordertaker')->with('ordertaker'); somthing like this i mean use model to get data into single veriable? – Hamza Qureshi Jan 22 '21 at 12:28
  • You should use get() for get the collection and find() for a single resource. – Kenath Jan 22 '21 at 12:40
  • @Joseph It returns the [collection](https://en.wikipedia.org/wiki/Collection_(abstract_data_type)) - not the array of collections. – Tpojka Jan 22 '21 at 13:40
1

You can't use find with get(Assuming that you need a one result not all of the users). Try this. But looks like you need build the relationships correctly first. Anyway, quick answer is below.

$userCols = User::select('column1','col2','col3')->find($id);
$subAdminCols = SubAdmin::select('col4','col5')->find($id);
$cols = array_merge($userCols->toArray(), $subAdminCols->toArray());
Kenath
  • 600
  • 5
  • 13
0

you can use php ... operator to to push data

example like this

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

return $subadmindata

ref link https://wiki.php.net/rfc/spread_operator_for_array

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
  • he asked how to make it with **laravel** collection, not the native way – Joseph Jan 22 '21 at 12:23
  • @Joseph i used this because he may have different table structure so – Kamlesh Paul Jan 22 '21 at 12:24
  • first of all thanx for every contributor but these methods i already know is there a way i use model function for this i have seen a query User::where('role', 5)->has('ordertaker')->with('ordertaker'); somthing like this i mean use model to get data into single veriable? – Hamza Qureshi Jan 22 '21 at 12:28
  • `array_push($subadmindata, ...$subadmindata1)` this looks wrong use of the ... operator. You can't use this like the spread operator in typescript. – Kenath Jan 22 '21 at 12:44
  • @Kenath https://stackoverflow.com/questions/45419150/php-spread-syntax-in-array-declaration check this – Kamlesh Paul Jan 22 '21 at 14:29
  • 1
    @KamleshPaul Looks like I'm wrong. I haven't tried with the php 7.4. Thanks for the clarification :) – Kenath Jan 22 '21 at 15:21
  • @Kenath i use php 8 for my project and i used all latest feature porvided buy that and i like the language – Kamlesh Paul Jan 22 '21 at 17:12
0

try this in user model

public function subadmin(){
        return $this->hasOne(SubAdmin::class,'sub_admin_id');
    }

in controller

    $sub_admins = User::find($id);
dd($sub_admins->subadmin->sub_admin_id)

   
Ostaad g
  • 301
  • 1
  • 7