How to remove duplicate rows from excel import in Laravel - excel

I am importing an excel file in Laravel 7 that contains employees' records. The file has Columns Employee No. and date. There are duplicate rows that contain the same employee No. and date. I want to store only 1 such record and discard the duplicate rows. Is there any way to do so?

If you're using laravel-excel, you can define rules for validation and check for duplicate records.
In below example, I've checked for duplication for user with email address.
class ModelImport implements ToModel, WithValidation
{
public function rules(): array
{
return [
'1' => \Illuminate\Validation\Rule::unique('users', 'email'),
];
}
}
Then, add skipping errors by implementing Maatwebsite\Excel\Concerns\SkipsOnFailure interface as stated in here
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithValidation;
class ModelImport implements ToModel, WithValidation, SkipsOnFailure
{
use Importable, SkipsFailures;
}

Related

How can i delete multiple row using laravel excel

I have a table with id,details.Field 'details' contain single family member details like name,age,dob etc.
table like,
|id|details|
|---|---|
|1|{'name':'john','age':'12','dob':'19-06-1987','address':'address','family_id':"1"}|
I want to delete multiple user details using excel file and data in details column (Id should not given).the excel file contain 3 column name,family_id (unique with each family),dob.i need to check those 3 field match with details column and delete the row.
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithValidation;
class ImportDeleteFile implements ToModel, WithValidation
{
use Importable;
public function rules(): array
{
return [
//what i do here
];
}

Exporting Laravel Model to Excel but with modification of created_at column to only date not datetime

i tried to modify a collection before returning it to be exported with maatwebsite Excel. The column i tried to modify is the created_date column (the default created_date of laravel) so that when exported to excel, i only get the date not datetime.
i tried:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
$temp = Invoice::all();
$len = count($temp);
for($i=0;$i<$len;$i+=1){
$temp[$i]->created_at = $temp[$i]->created_at->format('m/d/Y')
}
return $temp;
}
}
in the controller:
public function export()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx');
}
but, when exported, the result in the excel file is, for example: '07/26/2016T00:00:00.000000Z'
i noticed that the time become zero, and when i tried:
$temp[$i]->created_at = "some random string"
the laravel in webpage return error said that "some random string" cannot be parsed to datetime by Carbon Class constructor.
how can i make the exporting does not construct datetime with the string i give in the 'created_at' column, but just return the plain string instead? if let's say, i can't modify the database so i can't create extra column and extra column for this simple thing is too much, i think.
If you are using Maatwebsite\Excel 2.1 try column formatting by the setColumnFormat($array) method.
If Maatwebsite\Excel 3.1, try with the WithMapping interface.
created_at is marked as 'datetime' in the $cast property of your model by default so when you set it it's automatically cast as date (with Carbon::parse('the value you gave'))
If you want to export your collection with some values formatted you should not mutate the objects of the original objects but create new dedicated objects/arrays that will not interfere with model behavior nor corrupt your existing objects.

How to select data from excel sheet categorically in Flutter

I am a beginner in Flutter, I have a database of product items in a General Store in Excel file. I want to select the data from the excel database using their categories(the database has a column of categories)(such as Medicine or Automobile Items). Then I want to use the data in form of cards to show customers the product image and info. Please suggest me to how to select the data from an excel file (like ordering the data as per some condition).
WorkAround - I thought of converting .xlsx to .csv then to .json file and then finally storing it in Firebase which is the database I am currently using in my project. But the problem with that would be, the download usage would increase which would lead me to cost money.
You can create a class to store the data of your items. Then hard code items into a file and use that file to look up in your application.
For example;
// Data Structre
class Item {
int id;
String category;
Item({this.id, this.category});
}
// Hard coded data storage
List<Item> items = [Item(id: 1, category: 'Medicine'), Item(id: 2, category: 'Automobile')];
// Methods to fetch data
List<Item> getItemsByCategory(String category) {
return items.where((item) => item.category == category).toList();
}

Laravel excel export list in list object maatwebsite/Laravel-Excel

Hi I want to export list of people with all theirs childrens in them to excel. I'm using maatwebsite/Laravel-Excel plugin and its quite easy to export users collection but I need to include users every children inside that row. Its something like [{Name, Age, Gender, Job, Children[{name, age, gender, etc...}], Birthday, etc...}]. And how do I change order of the data column displayed on excel like if I want to switch datas on Age and Gender. Thank you!
You can use WithMapping
https://docs.laravel-excel.com/3.1/exports/mapping.html
<?php
public function map($user): array
{
//create initial array with user data in order that you want columns to be
$data = [
$user->name
...
];
// the same way you should crate headers (if you have). But instead of values you should create column name
foreach($user->children as $child){
array_push($data, $child->name, $child->birthday, $child->...., ..., ...); //and so on
}
// returned data should be 1 level array as each item is a row in the table
return $data;
}

SubSonic 3 / ActiveRecord - Easy way to compare two records?

With SubSonic 3 / ActiveRecord, is there an easy way to compare two records without having to compare each column by column. For example, I'd like a function that does something like this (without having to write a custom comparer for each table in my database):
public partial class MyTable
{
public IList<SubSonic.Schema.IColumn> Compare(MyTable m)
{
IList<SubSonic.Schema.IColumn> columnsThatDontMatch = new...;
if (this.Field1 != m.Field1)
{
columnsThatDontMatch.add(Field1_Column);
}
if (this.Field2 != m.Field2)
{
columnsThatDontMatch.add(Field2_Column);
}
...
return columnsThatDontMatch;
}
}
In the end, what I really need is a function that tests for equality between two rows, excluding the primary key columns. The pseudo-code above is a more general form of this. I believe that once I get the columns that don't match, I'll be able to check if any of the columns are primary key fields.
I've looked through Columns property without finding anything that I can use. Ideally, the solution would be something I can toss in the t4 file and generate for all my tables in the database.
The best way, if using SQL Server as your backend as this can be auto populated, is to create a derived column that has a definition that uses CHECKSUM to hash the values of "selected" columns to form a uniqueness outside of the primary key.
EDIT: if you are not using SQL Server then this hashing will need to be done in code as you save, edit the row.

Resources