0

I am beginner php developer. I make my project in Laravel 8.

I have this migrations:

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

If client.type = 1 - Client individual , 2 - Client Company

I have this code:

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',
    );

In my situation I have name - for company name, first_name for first name and last_name as last_name.

I need always one value "name":

  1. company = name
  2. individual user = name as 'first name + last name'

How can I make it?

Please help me

1
  • you can use accessor and mutator Commented Oct 12, 2020 at 17:43

1 Answer 1

1

you can use the accessor for that, for example


class YourModel extend Model
{
   //foo bla bla

  public function getFullNameAttribute()
  {
     return sprintf('%s %s',$this->name,$this->lastname;
  }
}

using


User::find(63);

echo $user->full_name;

relevent document

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I know this solution. However, I need to do it in joiny or selectRaw
for join u can collection override with a map or walk methods,laravel return collection from db everytime ... or u can concat is sql raw
DB::raw('CONCAT(lname ", ", lastanme) AS full_name
This is not exactly a solution. I need to have "name" written depending on whether the user is a company: client_company_infos.name oub if it is individual: name = client_infos.last_name + last_name
then you don't use if statement in the query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.