Is your CSV file compressed ?
Oct 17, 2022
8
Level 3
Getting the following try when uploading csv file to database --Could not find zip member zip://C:\Users\FARIRAI\Desktop\Orchid\storage\framework\cache\laravel-excel\laravel-excel-OSUUK3PLcKvTR2tgDfXhdDJF2yeDD94n.xlsx#_rels/.rels
below is my code im using laravel orchid n laravel excel the contact import
\namespace App\Imports;
use App\Models\Contact;
use Maatwebsite\Excel\Concerns\ToModel;
class ContactImport implements ToModel {
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row) {
// Check email already exists
$count = Contact::where('mobile',$row[2])->count();
if($count > 0){
return null;
}
return new Contact([
'name' => $row[0],
'mobile' => $row[1],
'account' => $row[2],
'description' => $row[3],
]);
}
the screen with the file upload option
\<?php
namespace App\Orchid\Screens\Examples;
use Illuminate\Http\Request;
use App\Models\Contact;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Mail;
use App\Orchid\Layouts\ExampleFieldsAdvancedLayout;
use Orchid\Screen\Fields\Input;
use Orchid\Screen\Fields\Quill;
use Orchid\Screen\Fields\Relation;
use Orchid\Screen\Fields\Textarea;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\Actions\Button;
use Orchid\Screen\Screen;
use App\Models\File;
use Orchid\Support\Facades\Alert;
use Orchid\Support\Facades\Toast;
use App\Imports\ContactImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class ExampleFieldsAdvancedScreen extends Screen
{
/**
*
* @return array
*/
public function query(): iterable
{
return [
'files' => File::filters()->defaultSort('id')->paginate(),
];
}
public function name(): ?string
{
return 'Bulk Add or Bulk Send';
}
// /**
// * Display header description.
// *
// * @return string|null
// */
public function description(): ?string
{
return 'Upload contact csv or create new.';
}
/**
* Button commands.
*
* @return Action[]
*/
public function commandBar(): iterable
{
return [
Button::make('Upload')
->method('store')
->icon('folder')
];
}
/**
* @param Request $request
*/
//upload the csv file
/**
* Views.
*
* @throws \Throwable
*
* @return \Orchid\Screen\Layout[]
*/
public function layout(): array
{
return [
Layout::rows([
Input::make('file')
->type('file')
->title('Upload To DB ')
->horizontal(),
Button::make('Upload')
->method('import')
->icon('folder'),
])->title('DB Direct Upload'),
Layout::rows([
Input::make('file')
->type('file')
->title('Upload File Path')
->horizontal(),
Button::make('Upload')
->method('store')
->icon('folder'),
])->title('CSV,TXT.Xls. Upload'),
Layout::rows([
Relation::make('contacts.')
->title('sender')
->multiple()
->required()
->placeholder('Mobile Number')
->help('Enter the user to send this message.')
->fromModel(Contact::class,'name','mobile'),
Relation::make('contacts.')
->title('recipients')
->multiple()
->required()
->placeholder('Mobile Number')
->help('Enter the users that you would like to send this message to.')
->fromModel(Contact::class,'name','mobile'),
TextArea::make('post.description')
->title('Enter the message you want to send')
->rows(3)
->maxlength(200)
->placeholder('Currently Flex is Down'),
Button::make('Send Message')
->method('sendMessage')
->icon('paper-plane'),
])->title('Send Message'),
ExampleFieldsAdvancedLayout::class,
];
}
public function store(Request $request){
$request->validate([
'file' => 'required|mimes:csv,txt,xlx,xls,pdf|max:2048'
]);
$fileModel = new File;
if($request->file()) {
$fileName = time().'_'.$request->file->getClientOriginalName();
$filePath = $request->file('file')->storeAs('uploads', $fileName, 'public');
$fileModel->name = time().'_'.$request->file->getClientOriginalName();
$fileModel->file_path = '/storage/' . $filePath;
$fileModel->save();
Toast::info('You have successfully uploaded a file.');
Alert::info('The file upload has been successful ♥.');
}
}
/**
* @param File $file
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function Remove(File $file)
{
$file->delete();
Alert::info('You have successfully deleted the contact.');
}
public function import()
{
Excel::import(new ContactImport, 'users.xlsx');
Toast::info('You have successfully uploaded to DB.');
}
}
Please or to participate in this conversation.