I am beginner laravel - developer.
I have this migrations:
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();
});
and models:
class Client extends Authenticatable implements Presentable { use Deactivation, Notifiable, 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;
}
}
Now I need select all record with client name:
- if company -> them client_company_infos.name
- if individual -> them client_infos.first_name and client_infos.last_name
I need use join or selectRaw.
How can I make it?
Please help me