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

46 lines
1.5 KiB
PHP
Raw Normal View History

2024-08-07 18:29:23 -05:00
<?php
namespace App\Imports\Sheets;
use App\Models\Portfolio;
2024-08-29 21:39:59 -05:00
use Illuminate\Validation\Rule;
2024-08-07 18:29:23 -05:00
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
{
// use Importable;
public function collection(Collection $portfolios)
{
2024-08-28 22:06:47 -05:00
foreach ($portfolios as $portfolio) {
2024-08-07 18:29:23 -05:00
2024-08-27 22:41:13 -05:00
auth()->user()->portfolios()
2024-08-29 21:39:59 -05:00
->where(['id' => $portfolio['portfolio_id']])
2024-08-27 22:41:13 -05:00
->orWhere(['title' => $portfolio['title']])
->firstOr(function () use ($portfolio) {
2024-08-07 18:29:23 -05:00
return Portfolio::make()->forceFill([
2024-08-29 21:39:59 -05:00
'id' => $portfolio['portfolio_id'] ?? null,
2024-08-27 22:41:13 -05:00
'title' => $portfolio['title'],
'wishlist' => $portfolio['wishlist'] ?? false,
'notes' => $portfolio['notes'],
2024-08-07 18:29:23 -05:00
])->save();
});
}
}
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
}