繁体   English   中英

在 Laravel 中使用两列表获取名称

[英]Get name with two columns tables in Laravel

我是初学者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();
        });

和型号:

类客户端扩展 Authenticatable 实现 Presentable { 使用停用、通知、SoftDeletes;

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。

我怎样才能做到?

请帮我

在这里试试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM