【发布时间】:2020-10-13 15:51:39
【问题描述】:
我是初学者 php 开发人员。 我在 Laravel 8 中制作我的项目。
我有这个迁移:
Schema::create('clients', function (Blueprint $table) {
$table->id();
$table->string('email');
$table->integer('type')->comment('1 - Client individual , 2 - Client Company');
$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();
});
如果 client.type = 1 - 客户个人,2 - 客户公司
我有这个代码:
Client::leftJoin('client_company_infos', 'client_company_infos.client_id', '=', 'clients.id')
->leftJoin('client_infos', 'client_infos.client_id', '=', 'clients.id')
->select('clients.*',
'client_company_infos.name as name',
'client_infos.first_name as first_name',
'client_infos.last_name as last_name',
);
在我的情况下,我有 name - 公司名称,first_name 为名字,last_name 为 last_name。
我总是需要一个值“名称”:
- 公司 = 名称
- 个人用户 = 名称为“名字 + 姓氏”
我该怎么做?
请帮帮我
【问题讨论】:
-
你可以使用访问器和修改器