Php 使用Laravel中的两列表获取名称
我是初学者laravel-开发者 我有以下迁移:Php 使用Laravel中的两列表获取名称,php,laravel,Php,Laravel,我是初学者laravel-开发者 我有以下迁移: Schema::create('clients', function (Blueprint $table) { $table->id(); $table->string('email'); $table->integer('type')->comment('1 - klient indywidualny, 2 - firma');
Schema::create('clients', function (Blueprint $table) {
$table->id();
$table->string('email');
$table->integer('type')->comment('1 - klient indywidualny, 2 - firma');
$table->string('crm_number')->unique();
$table->string('password');
$table->string('verify_token');
$table->rememberToken();
$table->timestamp('password_assigned_at')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->timestamps();
$table->deactivation();
$table->softDeletes();
$table->unique(['email', 'deleted_at']);
});
Schema::create('client_infos', function (Blueprint $table) {
$table->id();
$table->foreignId('client_id')->constrained('clients')->onDelete('cascade');
$table->string('first_name');
$table->string('last_name');
$table->string('phone_nr')->nullable();
$table->timestamps();
});
Schema::create('client_company_infos', function (Blueprint $table) {
$table->id();
$table->foreignId('client_id')->constrained('clients')->onDelete('cascade');
$table->string('name');
$table->string('nip')->nullable();
$table->string('phone_nr')->nullable();
$table->string('contact_email')->nullable();
$table->string('invoice_email')->nullable();
$table->timestamps();
});
和型号:
类客户端扩展Authenticatable实现可呈现
{
使用停用,
须呈报,
软删除
const INDIVIDUAL = 1;
const COMPANY = 2;
protected $fillable = [
'email',
'crm_number',
'email_verified_at',
'password_assigned_at',
'verify_token',
'password',
'type'
];
protected $hidden = [
'password',
'verify_token',
'remember_token',
];
protected $dates = [
'password_assigned_at',
'email_verified_at',
'created_at',
'updated_at',
'deactivated_at',
'deleted_at',
];
public function getAdminUrlAttribute(): AbstractAdminUrlPresenter
{
return new ClientUrlPresenter($this);
}
public function info()
{
return $this->hasOne(ClientInfo::class);
}
public function companyInfo()
{
return $this->hasOne(ClientCompanyInfo::class);
}
public function contractInfo()
{
return $this->hasOne(ClientContractInfo::class);
}
public function getHasAssignedPasswordAttribute(): bool
{
return !is_null($this->password_assigned_at);
}
public function getNameAttribute(): string
{
return $this->isCompany()
? data_get($this, 'companyInfo.name', "")
: data_get($this, 'info.full_name', "");
}
public function isIndividualClient()
{
return $this->type == self::INDIVIDUAL;
}
public function isCompany()
{
return $this->type == self::COMPANY;
}
}
现在我需要选择客户端名称为的所有记录:
$clients = Client::all()->map(function($client) {
if($client->type === Client::INDIVIDUAL) {
$client->name = $client->info->first_name . ' ' . $client->info->last_name;
} else if($client->type === Client::COMPANY) {
$client->name = $client->companyInfo->name;
}
return $client;
})
在这里试试这个:
$clients = Client::all()->map(function($client) {
if($client->type === Client::INDIVIDUAL) {
$client->name = $client->info->first_name . ' ' . $client->info->last_name;
} else if($client->type === Client::COMPANY) {
$client->name = $client->companyInfo->name;
}
return $client;
})
我有错误:“消息”:“尝试获取非对象的属性‘名称’,:(重构信息和公司信息中的关系…您应该在关系中分配一个表名称。我有错误:“消息”:“尝试获取非对象的属性‘名称’,:(重构您在信息和公司信息中的关系…您应该在关系中分配一个表名。到目前为止您尝试了什么?到目前为止您尝试了什么?