Import và Export dữ liệu từ database (ok)

https://docs.laravel-excel.com/3.1/getting-started/installation.html

composer require maatwebsite/excel

Ví dụ đã hoàn thành

routes\web.php

Route::post('/import-csv', [ProductController::class, 'import_csv'])->name('importcsv');
Route::post('/export-csv', [ProductController::class, 'export_csv'])->name('exportcsv');

app\Imports\ExcelImport.php

<?php
namespace App\Imports;
use App\Models\Excel;
use App\Models\CategoryProduct;
use Maatwebsite\Excel\Concerns\ToModel;
class ExcelImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new CategoryProduct([
            'category_name' => $row[0],
            'category_des' => $row[1],
            'category_status' => $row[2]
        ]);
    }
}

app\Exports\ExcelExport.php

<?php
namespace App\Exports;
use App\Models\Excel;
use App\Models\CategoryProduct;
use Maatwebsite\Excel\Concerns\FromCollection;
class ExcelExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return CategoryProduct::all();
    }
}

app\Models\CategoryProduct.php

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CategoryProduct extends Model
{
    use HasFactory;
    protected $table = 'tbl_category';
    protected $primaryKey = 'category_id';
    protected $fillable = [
        'category_name',
        'category_des',
        'category_status'
    ];
    public function product()
    {
        return $this->hasMany(Product::class,'category_id', 'category_id');
    }
}

resources\views\admin\all-category-product.blade.php

...
<form action="{{url('import-csv')}}" method="POST" enctype="multipart/form-data" style=" display: flex; align-items: center; margin: 20px; ">
    @csrf
    <input type="file" name="file" accept=".xlsx">
    <input type="submit" value="Import CSV" name="import_csv" class="btn btn-warning">
</form>
<form action="{{url('export-csv')}}" method="POST" style=" margin: 20px; ">
    @csrf
    <input type="submit" value="Export CSV" name="export_csv" class="btn btn-success">
</form>
...

Example 1: Import

routes\web.php

Route::get('/usersimport', [UserController::class, 'import'])->name('import');

app\Imports\UsersImport.php

<?php
namespace App\Imports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Facades\Hash;
class UsersImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new User([
            'name'     => $row[0],
           'email'    => $row[1],
           'password' => Hash::make($row[2]),
        ]);
    }
}

app\Http\Controllers\UserController.php

<?php
namespace App\Http\Controllers;
use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class UserController extends Controller {
    public function import()
    {
        Excel::import(new UsersImport, storage_path('users.xlsx'));
        return redirect('/')->with('success', 'All good!');
    }
}

Example 2: export

app\Http\Controllers\UserController.php

<?php
namespace App\Http\Controllers;
use App\Imports\UsersImport;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class UserController extends Controller {
    public function import()
    {
        Excel::import(new UsersImport, storage_path('users.xlsx'));
        return redirect('/')->with('success', 'All good!');
    }
    public function export()
    {
        Excel::download(new UsersExport, 'tests.xlsx');
        return redirect('/')->with('success', 'All good!');
    }
}

app\Exports\UsersExport.php

<?php
namespace App\Http\Controllers;
use App\Imports\UsersImport;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class UserController extends Controller {
    public function import()
    {
        Excel::import(new UsersImport, storage_path('users.xlsx'));
        return redirect('/')->with('success', 'All good!');
    }
    public function export()
    {
        return Excel::download(new UsersExport, 'tests.xlsx');
    }
}

routes\web.php

Route::get('/usersexport', [UserController::class, 'export'])->name('export');

Last updated