如何确保 laravel 中的列名相等?
How to ensure, that column names are equal in laravel?
我目前正在做一个 laravel php 项目。在这种情况下,应创建包含国家/地区的 table。 table 中包含三个字段:名为 country_code
的短国家代码列以及 country_name_de
和 country_code_en
列中的德语和英语国家名称。
这导致以下模型 class:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* Class Country
* @package App\Models
* @mixin \Illuminate\Database\Eloquent\Builder
*/
final class Country extends Model
{
use HasFactory;
protected $table = 'countries';
protected $primaryKey = 'country_code';
protected $keyType = 'string';
public $incrementing = false;
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
public $fillable = [
'country_code',
'country_name_de',
'country_name_en',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public static function getCountryCodeColumnName(): string
{
return 'country_code';
}
public static function getGermanCountryNameColumnName(): string
{
return 'country_name_de';
}
public static function getEnglishCountryNameColumnName(): string
{
return 'country_name_en';
}
public static function getTableName(): string
{
return 'countries';
}
}
此外,还有一个播种器可以将需要的值写入数据库。播种机当然需要列名来插入数据。目前我在播种机中使用此方法:
public function run()
{
// Load CSV-file country_codes.csv
if (($handle = fopen(storage_path('app/country_codes.csv'), 'r')) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$country = array(
Country::getCountryCodeColumnName() => $data[0],
Country::getGermanCountryNameColumnName() => $data[1],
Country::getEnglishCountryNameColumnName() => $data[2],
);
Country::create($country);
}
}
fclose($handle);
}
使用 'constants'/getters 来确保在引用 table 的特定列的任何 class 中没有拼写错误是一种很好的风格吗?是否也可以将这种样式用于迁移,或者有什么问题吗?
您的迁移不应依赖于任何 Model
参考。
实际上,如果您在模型中设置了 $fillable
,则不需要使用吸气剂,因为 Eloquent 会为您完成。
为确保正确性,您可以通过以下方式验证您的 table 是否具有必需的列:
$columns = Schema::getColumnListing('countries');
// check if $columns have all your required column
换一种方法,你可以这样做:
while (($data = fgetcsv($handle)) !== FALSE) {
$country = new Country();
$country->setCountryCode = $data[0];
$country->setCountryNameDe = $data[0];
$country->setCountryNameEn = $data[1];
$country->save();
}
这样您就不需要在模型中使用这些样板方法,eloquent 将填充模型 table 的各个字段。
我目前正在做一个 laravel php 项目。在这种情况下,应创建包含国家/地区的 table。 table 中包含三个字段:名为 country_code
的短国家代码列以及 country_name_de
和 country_code_en
列中的德语和英语国家名称。
这导致以下模型 class:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* Class Country
* @package App\Models
* @mixin \Illuminate\Database\Eloquent\Builder
*/
final class Country extends Model
{
use HasFactory;
protected $table = 'countries';
protected $primaryKey = 'country_code';
protected $keyType = 'string';
public $incrementing = false;
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
public $fillable = [
'country_code',
'country_name_de',
'country_name_en',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public static function getCountryCodeColumnName(): string
{
return 'country_code';
}
public static function getGermanCountryNameColumnName(): string
{
return 'country_name_de';
}
public static function getEnglishCountryNameColumnName(): string
{
return 'country_name_en';
}
public static function getTableName(): string
{
return 'countries';
}
}
此外,还有一个播种器可以将需要的值写入数据库。播种机当然需要列名来插入数据。目前我在播种机中使用此方法:
public function run()
{
// Load CSV-file country_codes.csv
if (($handle = fopen(storage_path('app/country_codes.csv'), 'r')) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$country = array(
Country::getCountryCodeColumnName() => $data[0],
Country::getGermanCountryNameColumnName() => $data[1],
Country::getEnglishCountryNameColumnName() => $data[2],
);
Country::create($country);
}
}
fclose($handle);
}
使用 'constants'/getters 来确保在引用 table 的特定列的任何 class 中没有拼写错误是一种很好的风格吗?是否也可以将这种样式用于迁移,或者有什么问题吗?
您的迁移不应依赖于任何 Model
参考。
实际上,如果您在模型中设置了 $fillable
,则不需要使用吸气剂,因为 Eloquent 会为您完成。
为确保正确性,您可以通过以下方式验证您的 table 是否具有必需的列:
$columns = Schema::getColumnListing('countries');
// check if $columns have all your required column
换一种方法,你可以这样做:
while (($data = fgetcsv($handle)) !== FALSE) {
$country = new Country();
$country->setCountryCode = $data[0];
$country->setCountryNameDe = $data[0];
$country->setCountryNameEn = $data[1];
$country->save();
}
这样您就不需要在模型中使用这些样板方法,eloquent 将填充模型 table 的各个字段。