3

I want to merge two array in laravel 5.3.I have variable '$type' which return

`Illuminate\Support\Collection Object ( [items:protected] => Array ( [1] => rinu )`

which is get from the query

$type0=DB::table('users')->where('name','=',$head)->pluck('name','id');

I want to merge with array $type1 which returns

Illuminate\Support\Collection Object ( [items:protected] => Array ( [2] => john ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [3] => dinesh ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( [4] => mahesh ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( ) )

$type1=DB::table('users')->where('name','=',$head)->pluck('name','id');

I tried to merge and store it in $type0;

$type0=$type0->merge($type1);

It return wrong value

Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh [3] => mahesh ) ) 
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh [3] => mahesh ) )`

enter code here

2 Answers 2

3

If you want to recieve merged array (and not collection), you can use toArray() and array_merge() functions:

$mergedArray = array_merge($type0->toArray(), $type1->toArray());
Sign up to request clarification or add additional context in comments.

1 Comment

caught FATALERROREXCEPTION IN 7BB347C8C194579700A647DADD62EA3D8E0AB68A.PHP LINE 358: CALL TO A MEMBER FUNCTION TOARRAY() ON ARRAY
1

You seem to have a Collection for each value. So you are probably fetching each value independently instead of in one go.

Your problem is probably solved by using whereIn instead of a number of wheres.

$result = DB::table('users')
            ->whereIn('name', ['John', 'Dinesh', 'Mahesh'])
            ->get();

Comments

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.