adds validation to imports

This commit is contained in:
hackerESQ
2024-08-29 21:39:59 -05:00
parent fc4f44ff05
commit 62f3ca6ef1
6 changed files with 68 additions and 12 deletions
+15 -3
View File
@@ -3,12 +3,14 @@
namespace App\Imports\Sheets;
use App\Models\Portfolio;
use Illuminate\Validation\Rule;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, SkipsEmptyRows
{
// use Importable;
@@ -17,12 +19,12 @@ class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
foreach ($portfolios as $portfolio) {
auth()->user()->portfolios()
->where(['id' => $portfolio['id']])
->where(['id' => $portfolio['portfolio_id']])
->orWhere(['title' => $portfolio['title']])
->firstOr(function () use ($portfolio) {
return Portfolio::make()->forceFill([
'id' => $portfolio['id'] ?? null,
'id' => $portfolio['portfolio_id'] ?? null,
'title' => $portfolio['title'],
'wishlist' => $portfolio['wishlist'] ?? false,
'notes' => $portfolio['notes'],
@@ -30,4 +32,14 @@ class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
});
}
}
public function rules(): array
{
return [
'portfolio_id' => ['sometimes', 'nullable'],
'title' => ['required', 'string'],
'wishlist' => ['sometimes', 'nullable', 'boolean'],
'notes' => ['sometimes', 'nullable', 'string'],
];
}
}