【问题标题】:Get name with two columns tables in Laravel在 Laravel 中使用两列表获取名称
【发布时间】:2026-01-21 22:00:01
【问题描述】:

我是初学者 laravel - 开发者。

我有这个迁移:

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();
        });

和模型:

class Client 扩展 Authenticatable 实现 Presentable { 使用停用, 应通知, 软删除;

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;
}

}

现在我需要选择所有带有客户名称的记录:

  1. 如果公司 -> 他们 client_company_infos.name
  2. 如果是个人 -> 他们是 client_infos.first_name 和 client_infos.last_name

我需要使用 join 或 selectRaw。

我该怎么做?

请帮帮我

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: php laravel


【解决方案1】:

在这里试试这个:

  $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;
    
})
  

【讨论】:

  • 我有错误:“消息”:“尝试获取非对象的属性'名称'”,:(
  • 重构您在 info 和 companyInfo 中的关系...您应该在关系中分配一个 table_name。