Created command for importing data from excel
This commit is contained in:
48
app/Console/Commands/ImportExcel.php
Normal file
48
app/Console/Commands/ImportExcel.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Imports\HasilBelajarsImport;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
|
||||
class ImportExcel extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'ba:import-excel {filename}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Import excel file to table';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
Excel::import(new HasilBelajarsImport, $this->argument('filename'));
|
||||
} catch (\Exception $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
40
app/Events/HasilBelajarImported.php
Normal file
40
app/Events/HasilBelajarImported.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\HasilBelajar;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class HasilBelajarImported
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $hasilBelajar;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param App\HasilBelajar $hasilBelajar
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(HasilBelajar $hasilBelajar)
|
||||
{
|
||||
$this->hasilBelajar = $hasilBelajar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return \Illuminate\Broadcasting\Channel|array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('channel-name');
|
||||
}
|
||||
}
|
||||
24
app/HasilBelajar.php
Normal file
24
app/HasilBelajar.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Events\HasilBelajarImported;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class HasilBelajar extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'siswa_id', 'meta',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'meta' => 'array',
|
||||
];
|
||||
|
||||
protected $dispatchesEvents = [
|
||||
'saved' => HasilBelajarImported::class,
|
||||
];
|
||||
}
|
||||
49
app/Imports/HasilBelajarsImport.php
Normal file
49
app/Imports/HasilBelajarsImport.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use App\Siswa;
|
||||
use App\HasilBelajar;
|
||||
use Maatwebsite\Excel\Concerns\ToModel;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
|
||||
class HasilBelajarsImport implements ToModel, WithHeadingRow
|
||||
{
|
||||
/**
|
||||
* @param array $row
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model|null
|
||||
*/
|
||||
public function model(array $row)
|
||||
{
|
||||
$siswa = Siswa::where('nisn', $row['nisn'])
|
||||
->where('nis', $row['nis'])->first();
|
||||
|
||||
if (!$siswa)
|
||||
return null;
|
||||
|
||||
$fieldMeta = [];
|
||||
foreach ($row as $key => $value) {
|
||||
if (preg_match("/\_+/", $key)) {
|
||||
$rowNames = explode('_', $key);
|
||||
$fieldMeta[$rowNames[0]][$rowNames[1]][$rowNames[2]] = round($value, 2);
|
||||
}
|
||||
else {
|
||||
$fieldMeta[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return new HasilBelajar([
|
||||
'siswa_id' => $siswa->id,
|
||||
'meta' => $fieldMeta,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sheets(): array
|
||||
{
|
||||
return [
|
||||
new FirstSheetImport()
|
||||
];
|
||||
}
|
||||
}
|
||||
32
app/Listeners/LogImportedHasilBelajar.php
Normal file
32
app/Listeners/LogImportedHasilBelajar.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Events\HasilBelajarImported;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogImportedHasilBelajar
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param object $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(HasilBelajarImported $event)
|
||||
{
|
||||
Log::channel('single')->debug($event->hasilBelajar);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Events\HasilBelajarImported;
|
||||
use App\Listeners\LogImportedHasilBelajar;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
@@ -18,6 +20,9 @@ class EventServiceProvider extends ServiceProvider
|
||||
Registered::class => [
|
||||
SendEmailVerificationNotification::class,
|
||||
],
|
||||
HasilBelajarImported::class => [
|
||||
LogImportedHasilBelajar::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user