php 如何在控制器中合并 Laravel 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14283532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:00:24  来源:igfitidea点击:

How to merge Laravel objects in controller

phplaravel

提问by sehummel

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.

我有一个控制器,我想将来自多个表的数据与并行结构组合在一起。我最终想要的是一个我可以从控制器返回的对象,以便我可以在 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 StackOverflowthat this works in PHP 5.3+. However, this returns the following error in Laravel:

StackOverflow 上另一篇文章告诉我,这适用于 PHP 5.3+。但是,这会在 Laravel 中返​​回以下错误:

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

How do I implement this method in Laravel? Both $mcand smreturn valid objects in Laravel.

我如何在 Laravel 中实现这个方法?双方$mcsm在Laravel返回有效对象。

回答by trm42

Nowadays you can use

现在你可以使用

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

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

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

这适用于Laravel 4,似乎可以处理数组和集合。

回答by Raftalks

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.

您可以在这里做的是合并两个查询结果的数组,然后使用 Response 和 json 输出,如下所示。

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

回答by Ramesh Kotkar

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

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

回答by Hassan Jamal

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().

这对我有用。使用 array_merge_recursive() 代替 array_merge。

回答by rashedcs

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

你可以简单地使用array_merge(firstObject,secondObject)函数。

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