sparclex / nova-import-card Goto Github PK
View Code? Open in Web Editor NEWA customizable resource import card for laravel nova
License: MIT License
A customizable resource import card for laravel nova
License: MIT License
One suggestion with the error output could be to make it a little more readable? i.e. instead of outputting the row index then the column that needs attention, it could output something like
The value field in row 5 is required
Currently this would be output as;
The 3.value field is required
With the heading row ignored then 0 index for the first data row, A user may not understand that 3 is in fact row 5 in their import file.
Originally posted by @kombimedia in #1 (comment)
I want to return Nova Action message depending upon import result, like I can write this in my own ImportHandler
:
return Action::danger('Something went wrong!');
Hi,
This package seems to be not compatible with laravel version 6
thx for response
support for laravel v7, need to upgrade illuminate/support
to atleast 7.0.
I have a situation where I wish to include an additional field in the card, but it seems that even though there was an intention to support customizing fields using a 'fields' meta field, it doesn't seem to be respected in the view.
Is anyone working on this?
There's an issue, if you allow for relations to be set during import.
Laravel\Nova\Rules\Relatable:49
-> $this->query->select('*')->whereKey($value)->first()
The query doesn't ever reset, so on the second row being added it will keep adding another primary key check like this: where
id= ? and
id = ?
.
This makes it fail
Returned error: Class 'App\Providers\App\Nova\User' not found
After default Laravel Nova installation
Then installing package via composer:
"composer require sparclex/nova-import-card"
Then registering in "NovaServiceProvider.php" file
public function card()
{
return [
// ...
new \Sparclex\NovaImportCard\NovaImportCard(App\Nova\User::class),
];
}
Application returned error:
Class 'App\Providers\App\Nova\User' not found
*/
public $width = '1/2';
public function __construct($resource)
{
parent::__construct();
$this->withMeta([
'fields' => [
new File('File'),
],
'resourceLabel' => $resource::label(),// --- This line returned error
Hi @Sparclex ,
Thanks for the nice card!
However I am having an issue importing to a resource that has a computed field, receive an 'illegal offset type error'. If I comment out the computed field in the resource, everything works as expected.
Error message:
Illegal offset type {"userId":4,"email":"[email protected]","exception":"[object] (ErrorException(code: 0): Illegal offset type at /home/vagrant/websites/shoeboxchristmas/vendor/laravel/nova/src/Fields/Field.php:362)
There also seems to be an issue importing to a 'BelongsTo' field, unless it is not marked as 'required' in the resource. I need this to be a required field though.
Any ideas on fixes or work arounds for these issues?
Also are you considering adding field mapping? That would be really nice.. : )
A sample import file is attached (had to save it as 'xlsx' to attach).
Thank you in advance for any assistance.
This is my resource;
`<?php
namespace App\Nova;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Http\Requests\NovaRequest;
use App\Nova\Metrics\Children;
use App\Nova\Metrics\Donators;
use App\Nova\Metrics\RegisteredShoeBoxes;
use Sparclex\NovaImportCard\NovaImportCard;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Fields\Select;
use Naif\Toggle\Toggle;
use App\School;
use Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel;
class Child extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\Child';
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id', 'name', 'donator_id', 'contact_name', 'contact_number',
];
/**
* The relationships that should be eager loaded on index queries.
*
* @var array
*/
public static $with = ['school', 'donator'];
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()
->sortable()
->hideFromIndex(),
Text::make('Ref', function () {
$id = $this->school_id;
$schools = School::where('id', $id)->get();
foreach ($schools as $school) {
return $school->code. '' .$this->id;
}
}),
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
BelongsTo::make('School')
->rules('required'),
Text::make('Age')
->sortable()
->rules('required', 'max:3'),
Select::make('Gender')
->options([
'Girl' => 'Girl',
'Boy' => 'Boy',
])
->sortable()
->rules('required'),
Textarea::make('Notes')
->rules('max:500'),
Text::make('Contact Name')
->rules('required', 'max:255'),
Text::make('Contact Number')
->rules('required', 'max:55'),
BelongsTo::make('Donator')
->hideWhenCreating()
->searchable(),
Toggle::make('Allocated')
->color('#3C8D0D')
->hideWhenCreating()
->sortable(),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [
(new Donators)->width('1/3'),
(new Children)->width('1/3'),
(new RegisteredShoeBoxes)->width('1/3'),
new \Sparclex\NovaImportCard\NovaImportCard(Child::class),
];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [
(new DownloadExcel)
->withHeadings()
->allFields()
->except('school_code', 'donator_id', 'deleted_at', 'created_at', 'updated_at')
->askForFilename(),
];
}
}
`
Perhaps being able to set the sizing of the card and the label would be helpful.
This way if you end up needing 2 imports (for some reason like legacy reasons) on a single resource you don't wind up with the same named import cards.
A not so convoluted example would be like importing information about the resource and then perhaps pricing data if those 2 datasets come from different departments. It already works to just create 2 based off of different resources and use a custom importer, but the label on the "Import RESOURCE" just makes it so you have to create a whole resource for no reason.
Is this possible?
I have Cities that hasMany Users and User belongsTo a City.
I want to have an import card onlyInDetail of City resource that say Import Users.
And now, in excel file i do not want to include the city_id, i need to pass as parameter so all Users imported will belong to the City that the Import is executed.
In documentation what your package use is just the full path of class using Namespace: App\Imports\UserImport::class or 'App\Imports\UserImport'
Instead i want: new UserImport($city_id); lets say
If this is possible please tell me how to pass the parameter, if not, it will be great to have this as feature.
Thank you, great package btw
Say I have imported something with fields unique_id and value.
How do I make it so that when I import the same unique_id again, the model will be updated in the database instead of giving a duplicate unique key error (because it tries to create a new one)?
Hi,
I would like to extends BasicImporter because i have 2 more fields in my database than in my Excel so i would like to extends BasicImporter and i did that:
php
namespace App\Imports;
use App\Statement;
use Sparclex\NovaImportCard\BasicImporter;
class StatementsImport extends BasicImporter {
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Statement|null
*/
public function model(array $row)
{
return new Statement([
'date_publishing' => date("Y-m-d", strtotime($row[0])),
'date_value' => date("Y-m-d", strtotime($row[1])),
'operation' => str_replace("\\n", "\\r", $row[2]),
'debit' => abs($row[3]),
'credit' => abs($row[4]),
]);
}
}
But i still have error regarding missing fields (pointable & pointable_type) ... Let me know if you have a good example about Extend BasicImporter .. i don't know which kind of informations you need in attributes or rules or regarding Model
my data in Excel (d/m/Y for date)
my date in database: (Y-m-d for date)
my Migration :
Schema::create('statements', function (Blueprint $table) {
$table->increments('id');
$table->date('date_publishing');
$table->date('date_value');
$table->text('operation');
$table->float('debit')->default(0);
$table->float('credit')->default(0);
$table->integer('pointable_id')->nullable();
$table->string('pointable_type')->nullable();
$table->timestamps();
$table->softDeletes();
});
my Model:
class Statement extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'statements';
protected $fillable = ['date_publishing','date_value','operation','debit','credit'];
//Casts of the model dates
protected $casts = [
'date_publishing' => 'date',
'date_value' => 'date'
];
public function pointable()
{
return $this->morphTo();
}
Thanks in advance,
Rémy
I'm putting two of these on my dashboard page (e.g. one for users, one for product vendors) and when I do, picking a file for the second box puts it in the first, and picking a file for the first box puts it in the first.
A quick glance at the page's source suggests that the input's name
HTML attribute is the same across all instances, there's a conflict going on there. Perhaps making a unique name for the field would work, such as name_product_vendor
?
EDIT: Steps to reproduce:
the value from belongsto field is always considered empty. why?
After having run the import, trigger the table to reload its data.
Can the same styling be applied to the card title as default nova cards - make it
<h3 class="mr-3 text-base text-80 font-bold">Import Products</h3>
At the moment only one validation message is displayed due to the constraints of the card height. It should be possible to view all validation errors as mentioned in #1 (comment)
A tool with multiple import cards as tabs would be nice
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.