Laravel 修改集合数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39751739/
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
Laravel Modify collection data
提问by user2636197
I wonder if Laravel have any helper to modify a collection. What I need to do is to make a query with paginate() then check if the logged in users ID match the sender or reciever and based on that add a new value to the output:
我想知道 Laravel 是否有任何帮助来修改集合。我需要做的是使用 paginate() 进行查询,然后检查登录的用户 ID 是否与发送者或接收者匹配,并基于此向输出添加新值:
$userId = Auth::guard('api')->user()->user_id;
$allMessages = Conversation::join('users as sender', 'conversations.sender_id', '=', 'sender.user_id')
->join('users as reciver', 'conversations.recipient_id', '=', 'reciver.user_id')
->where('sender_id',$userId)->orWhere('recipient_id',$userId)
->orderBy('last_updated', 'desc')
->select('subject','sender_id','recipient_id','sender_unread','recipient_unread','last_updated','reciver.username as receivername','sender.username as sendername')
->paginate(20);
Now I want to do something like:
现在我想做一些类似的事情:
if ( $allMessages->sender_id == $userId ) {
//add new value to output
newField = $allMessages->sendername
} else {
//add new value to output
newField = $allMessages->receivername
}
Then send the data with the new value added
然后发送添加了新值的数据
return response()->json(['messages' => $allMessages],200);
Is this possible?
这可能吗?
回答by ceejayoz
You're better off using the Collection class's built-in functions for this. For example, the map
function would be perfect.
为此,您最好使用 Collection 类的内置函数。例如,该map
功能将是完美的。
https://laravel.com/docs/5.3/collections#method-map
https://laravel.com/docs/5.3/collections#method-map
$allMessages = $allMessages->map(function ($message, $key) use($userId) {
if ($message->sender_id == $userId) {
$message->display_name = $message->receivername;
} else {
$message->display_name = $message->sendername;
}
return $message;
});
回答by user2636197
Solved by adding:
通过添加解决:
foreach ($allMessages as $message) {
if ($message->sender_id == $userId) {
$message->display_name = $message->receivername;
} else {
$message->display_name = $message->sendername;
}
}
回答by Saumya Rastogi
As I don't know which Laravel version you're using, taking Laravel 5.2 let me give you a smarter way to deal with this (if I get your problem correctly).
因为我不知道您使用的是哪个 Laravel 版本,所以使用 Laravel 5.2 让我给您一个更聪明的方法来处理这个问题(如果我正确理解了您的问题)。
You can use Laravel's LengthAwarePaginatior(API Docs).
您可以使用 Laravel 的LengthAwarePaginatior( API Docs)。
Don't use paginate method when you are bulding your query, instead of that use simple get method to get simple collection.
在构建查询时不要使用 paginate 方法,而不要使用简单的 get 方法来获取简单的集合。
$userId = Auth::guard('api')->user()->user_id;
$allMessages = Conversation::join('users as sender', 'conversations.sender_id', '=', 'sender.user_id')
->join('users as reciver', 'conversations.recipient_id', '=', 'reciver.user_id')
->where('sender_id',$userId)->orWhere('recipient_id',$userId)
->orderBy('last_updated', 'desc')
->select('subject','sender_id','recipient_id','sender_unread','recipient_unread','last_updated','reciver.username as receivername','sender.username as sendername')
->get();
Now you can populate extra items into that collection based on your certain conditions like this.
现在,您可以根据这样的特定条件将额外的项目填充到该集合中。
if ($allMessages->sender_id == $userId ) {
// add new value to collection
} else {
// add new value to collection
}
Now use LengthAwarePaginator, to convert that populated collection into a paginated collection.
现在使用 LengthAwarePaginator 将填充的集合转换为分页集合。
$total_count = $allMessages->count();
$limit = 20;
$current_page = request()->get('page');
$options = [
'path' => request()->url(),
'query' => request()->query(),
];
$paginated_collection = new LengthAwarePaginator($allMessages, $total_count, $limit, $current_page, $options);
The variable $paginated_collection
now can be used to be sent in response. Hope this helps you to deal with your problem.
$paginated_collection
现在可以使用该变量作为响应发送。希望这可以帮助您处理您的问题。