LARAVEL 5.7:gotget()和exception()仍然不能删除Collection中的项目

时间:2019-02-05 12:35:04

标签: laravel

https://imgur.com/a/TokTAsq

为什么不forget()从集合中删除created_at。尝试过except()也不起作用

$descRequest = request()->descTable;
$wherePage = request()->onPage ?? 1;
$rowsPerPageRequest = request()->rowPerPage ?? 2;
$sortedByRequest = request()->sortBy ?? 'generatednumber_id';

$makeData = Generatenumberid::withCount([
        'generateNumberIdGeneratedNumber as total_generated_number'
    ])
    ->orderBy($sortedByRequest, $descRequest)
    ->paginate($rowsPerPageRequest, ['*'], 'page', $wherePage);

$mapMakeData = $makeData->map(function ($item, $key) {
    $carbonDateString = $item->created_at->format('d F Y');
    $carbonForHuman = $item->created_at->diffForHumans();

    $item->id = $item->id;
    $item->generatednumber_id = $item->generatednumber_id;
    $item->total_generated_number = $item->total_generated_number;
    $item->time_generated = "{$carbonDateString} - {$carbonForHuman}";

    return $item;
});

$collectMap = collect($mapMakeData->all());
$collectMap->forget('created_at');

dd($collectMap->all());

预期结果

给定的列已从集合中删除

"id" => 39
"generatednumber_id" => "CELLNUMBS-|2019-02-03|-0023"
"updated_at" => "2019-02-03 13:25:01"
"total_generated_number" => 0
"time_generated" => "03 February 2019 - 1 day ago"

1 个答案:

答案 0 :(得分:0)

我认为这是因为您正在处理数组的集合。 forget()except()仅适用于集合。

因此,这应该可行。

//
//
//

$collectMap = collect($mapMakeData->all());
$collectMap->map(function($item) {
    return array_except($item, 'created_at');
});

dd($collectMap->all());