31

I have a controller where I want to combine data from multiple tables with parallel structures. What I want to end up with in the end is one object I can return from the controller so I can parse it in Backbone.

I want to do something like this:

public function index()
{
    $mc = MainContact::where('verified', '=', '1')->get();
    $sm = SendMessage::where('verified', '=', '1')->get();

    $obj = (object) array_merge((array) $mc, (array) $sm);
    return $obj;
}

I'm told by another post on StackOverflow that this works in PHP 5.3+. However, this returns the following error in Laravel:

UnexpectedValueException: The Response content must be a string or object implementing
 __toString(), "object" given.

How do I implement this method in Laravel? Both $mc and sm return valid objects in Laravel.

4
  • I'm using the Laravel 4 alpha right now. I'm going to upgrade to the beta when it comes out today or Monday or whenever it comes out. Commented Jan 11, 2013 at 17:54
  • I tried creating a separate class with a blank __toString() method in it but don't know where to store it in Laravel 4. Commented Jan 11, 2013 at 18:07
  • It looks like the class goes in the vendor folder Commented Jan 11, 2013 at 18:28
  • What is the path to Eloquent from the vendors folder? Commented Jan 11, 2013 at 18:48

6 Answers 6

53

Nowadays you can use

$new_collection = $collection->merge($other_collection).

This works in Laravel 4 and seems to handle both arrays and collections.

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

4 Comments

In my scenario, it eats up some rows after the collection merge.
Is your collection using strings as keys? Collection::merge uses PHP's array_merge, which overwrites string keys but not integer keys. If that's a problem, you can always subclass Laravel Collection to override the implementation.
In my case, only the $other_collection remains and the $collection disappears. Why???
@AliGajani, yes it does. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection. But If the given items's keys are numeric, the values will be appended to the end of the collection. Check it out here: laravel.com/docs/5.7/collections#method-merge
36

What you can do here is merge the arrays of the two query result and then use the Response with json output like shown below.

$array = array_merge($mc->toArray(), $sm->toArray());
return Response::json($array);

Comments

10

We can use collection as below

$admins = User::where('type', '=', 'admin')->get();

$authors = User::where('type', '=', 'author')->get();

$admin_author_collection = $admins->merge($authors);

Also, Please refer the various collection methods to below link

http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html

Comments

3
Route::get('test', function(){
    $rank = Rank::get();
    $policy = Policy::get();
    $obj = (object)array_merge_recursive((array)$rank , (array)$policy);
    var_dump($obj);
});

This is working for me. Instead of array_merge use array_merge_recursive().

Comments

0

I use concat and works for me. like this:

$role1 = User::where('role', '=', 'role1')->get();
$role2 = User::where('role', '=', 'role2')->get();
$allRoles = $role1->concat($role2);

Comments

-4

You could simply use array_merge(firstObject,secondObject) function.

$obj = array_merge($mc, $sm);
return $obj;

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.