Lompat ke konten Lompat ke sidebar Lompat ke footer

cara Import data excel to db database laravel 10

 


fitur ini nambahin bukan update data ngab

 

1. composer require psr/simple-cache:^1.0 maatwebsite/excel --ignore-platform-reqs --with-all-dependencies

 

2. php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"

 

edit bagian config/app .php

'providers' => ServiceProvider::defaultProviders()->merge([
.....
        Maatwebsite\Excel\ExcelServiceProvider::class,


    'aliases' => Facade::defaultAliases()->merge([
        // 'Example' => App\Facades\Example::class,

        'Excel' => Maatwebsite\Excel\Facades\Excel::class,
    ])->toArray(),

 

3. bikin view users.blade.php

<!DOCTYPE html>

<html>

<head>

    <title>mangeak ? nambahin data paten no uno</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

     

<div class="container">

    <div class="card bg-light mt-3">

        <div class="card-header">

        mangeak ? nambahin data paten no uno

        </div>

        <div class="card-body">

            <form action="{{ route('users.import') }}" method="POST" enctype="multipart/form-data">

                @csrf

                <input type="file" name="file" class="form-control">

                <br>

                <button class="btn btn-success">Import User Data</button>

            </form>

 

            <table class="table table-bordered mt-3">

                <tr>

                    <th colspan="3">

                        List Of Users

                        <a class="btn btn-warning float-end" href="{{ route('users.export') }}">Export User Data</a>

                    </th>

                </tr>

                <tr>

                    <th>ID</th>

                    <th>Kode Supplier</th>

                    <th>Nama Supplier</th>
                    <th>Telepon</th>
                    <th>flag</th>
                    <th>Unit Kerja</th>
                    <th>Create AT</th>
                    <th>Update AT</th>

                </tr>

                @foreach($suppliers as $suppliers)

                <tr>

                    <td>{{ $suppliers->id }}</td>

                    <td>{{ $suppliers->kode_supplier }}</td>

                    <td>{{ $suppliers->nama_supplier }}</td>

                    <td>{{ $suppliers->telepon }}</td>
                    <td>{{ $suppliers->flag }}</td>
                    <td>{{ $suppliers->unit_kerja_id }}</td>
                    <td>{{ $suppliers->created_at }}</td>
                    <td>{{ $suppliers->updated_at }}</td>

                </tr>

                @endforeach

            </table>

 

        </div>

    </div>

</div>

     

</body>

</html>


4. bikin controller di Imports/UsersImport.php

<?php

 

namespace App\Imports;

 

use App\Models\Supplier;

use Maatwebsite\Excel\Concerns\ToModel;

use Maatwebsite\Excel\Concerns\WithHeadingRow;

use Hash;

 

class UsersImport implements ToModel, WithHeadingRow

{

    /**

    * @param array $row

    *

    * @return \Illuminate\Database\Eloquent\Model|null

    */

    public function model(array $row)

    {

        return new Supplier([

            'kode_supplier'     => $row['kode_supplier'],

            'nama_supplier'    => $row['nama_supplier'],

            'telepon'    => $row['telepon'],
            'flag'    => $row['flag'],
            'unit_kerja_id'    => $row['unit_kerja_id'],
            'created_at'    => $row['created_at'],
            'updated_at'    => $row['updated_at'],

        ]);

    }

}

5. bikin controller export di Exports/ExportUser.php

<?php

namespace App\Exports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class ExportUser implements FromCollection {
    public function collection()
    {
        return User::select('kode_supplier','nama_supplier','password','jabatan','path','foto','role','slug','flag','unit_kerja_id','email_verified_at','remember_token','created_at','updated_at')->get();
    }
}
?>



6. bikin controller lagi di Controller/UserController.php

<?php

 

namespace App\Http\Controllers;

 

use Illuminate\Http\Request;

use App\Exports\UsersExport;

use App\Imports\UsersImport;

use Maatwebsite\Excel\Facades\Excel;

use App\Models\Supplier;


 

class UserController extends Controller

{

    /**

    * @return \Illuminate\Support\Collection

    */

    public function index()

    {

        $suppliers = Supplier::get();

 

        return view('users', compact('suppliers'));

    }

       

    /**

    * @return \Illuminate\Support\Collection

    */

    public function export()

    {

        return Excel::download(new UsersExport, 'users.xlsx');

    }

       

    /**

    * @return \Illuminate\Support\Collection

    */

    public function import()

    {

        Excel::import(new UsersImport,request()->file('file'));

               

        return back();

    }

}


7. bagian web.php

use App\Http\Controllers\UserController;
Route::controller(UserController::class)->group(function(){

  Route::get('users', 'index');

  Route::get('users-export', 'export')->name('users.export');

  Route::post('users-import', 'import')->name('users.import');

});


8. buka 

http://127.0.0.1:8000/users