Skip to content

Commit

Permalink
Merge pull request #47 from morcov/fixing-belong-to-relation
Browse files Browse the repository at this point in the history
Fixing belong to relation
  • Loading branch information
simonhamp authored Aug 14, 2023
2 parents 56b4f3c + caf34a4 commit c976287
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laravel\Nova\Resource;
use Laravel\Nova\Rules\Relatable;
use Maatwebsite\Excel\Concerns\ToModel as ModelImporter;
use SimonHamp\LaravelNovaCsvImport\Http\Requests\ImportNovaRequest;

class ImportController
{
Expand All @@ -26,7 +27,7 @@ public function __construct(ModelImporter $importer, Filesystem $filesystem)
$this->filesystem = $filesystem;
}

public function configure(NovaRequest $request, string $file): Response
public function configure(ImportNovaRequest $request, string $file): Response
{
$file_name = pathinfo($file, PATHINFO_FILENAME);

Expand Down Expand Up @@ -125,7 +126,7 @@ public function preview(NovaRequest $request, string $file): Response
);
}

public function import(NovaRequest $request)
public function import(ImportNovaRequest $request)
{
$file = $request->input('file');

Expand All @@ -138,6 +139,8 @@ public function import(NovaRequest $request)
$resource_name = $config['resource'];

$resource = Nova::resourceInstanceForKey($resource_name);

$request->setImportResource(get_class($resource));
$rules = $this->extractValidationRules($resource, $request)->toArray();
$model_class = $resource->resource::class;

Expand Down Expand Up @@ -193,7 +196,7 @@ public function review(NovaRequest $request, string $file): Response
);
}

protected function getAvailableFieldsForImport(string $resource, NovaRequest $request): array
protected function getAvailableFieldsForImport(string $resource, ImportNovaRequest $request): array
{
$novaResource = new $resource(new $resource::$model);
$fieldsCollection = collect($novaResource->creationFields($request));
Expand All @@ -205,6 +208,8 @@ protected function getAvailableFieldsForImport(string $resource, NovaRequest $re
}

$fields = $fieldsCollection->map(function (Field $field) use ($novaResource, $request) {
$request->setImportResource($novaResource);

return [
'name' => $field->name,
'attribute' => $field->attribute,
Expand Down
39 changes: 39 additions & 0 deletions src/Http/Requests/ImportNovaRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace SimonHamp\LaravelNovaCsvImport\Http\Requests;

use Laravel\Nova\Http\Requests\NovaRequest;

class ImportNovaRequest extends NovaRequest
{
/**
* @var
*/
protected $importResource;

/**
* @param mixed $resource
*/
public function setImportResource($resource): self
{
$this->importResource = $resource;

return $this;
}

/**
* Get the class name of the resource being requested.
*
* @return class-string<\Laravel\Nova\Resource>
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function resource()
{
return tap(once(function () {
return $this->importResource;
}), function ($resource) {
abort_if(is_null($resource), 404);
});
}
}

0 comments on commit c976287

Please sign in to comment.