Files
investbrain/app/Imports/Sheets/PortfoliosSheet.php
T

34 lines
1.1 KiB
PHP
Raw Normal View History

2024-08-07 18:29:23 -05:00
<?php
namespace App\Imports\Sheets;
use App\Models\Portfolio;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
{
// use Importable;
public function collection(Collection $portfolios)
{
2024-08-27 22:41:13 -05:00
foreach ($portfolios->sortBy('date') as $portfolio) {
2024-08-07 18:29:23 -05:00
2024-08-27 22:41:13 -05:00
auth()->user()->portfolios()
->where(['id' => $portfolio['id']])
->orWhere(['title' => $portfolio['title']])
->firstOr(function () use ($portfolio) {
2024-08-07 18:29:23 -05:00
return Portfolio::make()->forceFill([
2024-08-27 22:41:13 -05:00
'id' => $portfolio['id'] ?? null,
'title' => $portfolio['title'],
'wishlist' => $portfolio['wishlist'] ?? false,
'notes' => $portfolio['notes'],
2024-08-07 18:29:23 -05:00
])->save();
});
}
}
}