欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel array_merge collection How to merge Laravel objects in controller



1)使用 $collection1->merge($collection1);  
见 https://docs.golaravel.com/docs/5.4/collections/ 





2)用 toArray() 见下面

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.

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.


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.



 

正确答案
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);
shareimprove this answer
 
   
That did the trick. Thanks, Raf. – sehummel Jan 11 '13 at 21:33

Nowadays you can use

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

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

shareimprove this answer
 
1 
In my scenario, it eats up some rows after the collection merge. – Ali Gajani Mar 2 '15 at 17:46
   
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. – trm42 Mar 3 '15 at 8:12
   
In my case, only the $other_collection remains and the $collection disappears. Why??? – PathrosJun 6 '16 at 18:09

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

shareimprove this answer
 
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().

shareimprove this answer
 

来自  https://stackoverflow.com/questions/14283532/how-to-merge-laravel-objects-in-controller


普通分类: