Trong trường hợp tệp của bạn chứa một hàng tiêu đề [một hàng trong đó mỗi ô chỉ ra mục đích của cột đó] và bạn muốn sử dụng các tên đó làm khóa mảng của mỗi hàng, bạn có thể thực hiện mối quan tâm của WithHeadingRow
.
Cho rằng chúng tôi có một tệp Excel trông như thế này:
Patrick Brouwers | Một số giá trị |
Bây giờ chúng ta có thể tham chiếu tiêu đề thay vì khóa mảng số.
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
'at' => $row['at_field'],
]];
}
}
1234567891011121314151617
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Hướng hàng trên hàng khác nhau
Trong trường hợp hàng tiêu đề của bạn không nằm ở hàng đầu tiên, bạn có thể dễ dàng chỉ định điều này trong lớp nhập của mình:
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
]];
}
public function headingRow[]: int
{
return 2;
}
}
123456789101112131415161718192021
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Hàng thứ 2 bây giờ sẽ được sử dụng làm hàng tiêu đề.
# Định dạng khóa tiêu đề
Theo mặc định, các phím tiêu đề được định dạng với Trình trợ giúp Laravel str_slug[]
. Ví dụ. Điều này có nghĩa là tất cả các không gian được chuyển đổi thành
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
]];
}
public function headingRow[]: int
{
return 2;
}
}
0.Nếu bạn muốn thay đổi hành vi này, bạn có thể làm như vậy bằng cách mở rộng
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
]];
}
public function headingRow[]: int
{
return 2;
}
}
1# Không định dạng
Nếu bạn không muốn định dạng, bạn có thể sử dụng định dạng
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
]];
}
public function headingRow[]: int
{
return 2;
}
}
2. Các phím mảng sẽ chứa dữ liệu chính xác ở hàng tiêu đề.use Maatwebsite\Excel\Imports\HeadingRowFormatter;
HeadingRowFormatter::default['none'];
public function model[array $row]
{
return new User[[
'name' => $row['Name'],
'email' => $row['Email'],
]];
}
1234567891011
2
3
4
5
6
7
8
9
10
11
# Định dạng tùy chỉnh
Bạn có thể xác định một định dạng tùy chỉnh với
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
]];
}
public function headingRow[]: int
{
return 2;
}
}
3 trong nhà cung cấp dịch vụ.HeadingRowFormatter::extend['custom', function[$value, $key] {
return 'do-something-custom' . $value;
// And you can use heading column index.
// return 'column-' . $key;
}];
123456
2
3
4
5
6
Bạn có thể đặt định dạng tùy chỉnh trong
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
]];
}
public function headingRow[]: int
{
return 2;
}
}
4.'imports' => [
'heading_row' => [
'formatter' => 'custom',
],
],
12345
2
3
4
5
Hoặc sau đó bạn có thể đặt định dạng mới này trong một nhà cung cấp dịch vụ.
HeadingRowFormatter::default['custom'];
1
# Chỉ nhập hàng tiêu đề
Đôi khi bạn có thể muốn tìm kiếm trước hàng tiêu đề để thực hiện một số xác nhận. Chúng tôi có một lối tắt dễ dàng cho việc này:
namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
public function model[array $row]
{
return new User[[
'name' => $row['name'],
'email' => $row['email'],
]];
}
public function headingRow[]: int
{
return 2;
}
}
5.use Maatwebsite\Excel\HeadingRowImport;
class UsersImportController extends Controller
{
public function import[]
{
$headings = [new HeadingRowImport]->toArray['users.xlsx'];
}
}
123456789
2
3
4
5
6
7
8
9
Mảng tiêu đề chứa một mảng các tiêu đề trên mỗi tờ.
# Nhóm các giá trị của nhiều cột chia sẻ cùng một tiêu đề
Cho rằng chúng tôi có một tệp Excel trông như thế này:
Patrick Brouwers | Một số giá trị | Bây giờ chúng ta có thể tham chiếu tiêu đề thay vì khóa mảng số. |
# Hướng hàng trên hàng khác nhau
[
'name' => 'Patrick Brouwers',
'email' => '',
'options' => [
'Some value',
'Some other value'
]
]
12345678
2
3
4
5
6
7
8