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

41 lines
1.2 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;
2024-08-29 21:39:59 -05:00
use Maatwebsite\Excel\Concerns\WithValidation;
2024-08-07 18:29:23 -05:00
2024-08-29 21:39:59 -05:00
class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, SkipsEmptyRows
2024-08-07 18:29:23 -05:00
{
public function collection(Collection $portfolios)
{
2024-08-30 20:22:28 -05:00
foreach ($portfolios as $portfolio) {
Portfolio::unguard();
2024-08-07 18:29:23 -05:00
2024-08-30 20:22:28 -05:00
Portfolio::updateOrCreate([
'id' => $portfolio['portfolio_id']
], [
'id' => $portfolio['portfolio_id'] ?? null,
'title' => $portfolio['title'],
'wishlist' => $portfolio['wishlist'] ?? false,
'notes' => $portfolio['notes'],
]);
}
2024-08-07 18:29:23 -05:00
}
2024-08-29 21:39:59 -05:00
public function rules(): array
{
return [
'portfolio_id' => ['sometimes', 'nullable'],
'title' => ['required', 'string'],
'wishlist' => ['sometimes', 'nullable', 'boolean'],
'notes' => ['sometimes', 'nullable', 'string'],
];
}
2024-08-07 18:29:23 -05:00
}