0

I have got data of my projects in laravel with ...

$aProjects = Project::all();

i want to get full name of project creator with...

foreach ($aProjects as $key => $project) {
    $sFullName = $project->user->getFullName();
    $project->fullName = $sFullName;
}

although all projects data are the same , only for first $project i have got fullName. for others i have got null

this is getFullName() function in User Model

public function getFullName()
{
    return $this->firstName ." ".$this->lastName;
}

whit this foreach

$a = [] ;
foreach ($aProjects as $key => $project) {
    $a[$key]= $project->user;
}
dd($a);

laravel return

array:9 [
  0 => App\User {#330
    #fillable: array:5 [
      0 => "firstName"
      1 => "lastName"
      2 => "userName"
      3 => "email"
      4 => "password"
    ]
    #hidden: array:2 [
      0 => "password"
      1 => "remember_token"
    ]
    #casts: array:1 [
      "users_email_verified_at" => "datetime"
    ]
    #connection: "mysql"
    #table: "users"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:10 [
      "id" => 1
      "firstName" => "komail"
      "lastName" => "fayazbakhsh"
      "role" => 1
      "email" => "komail.f67@gmail.com"
      "email_verified_at" => null
      "password" => "$2y$10$M5mRU1QkZ84nSkkalAlyVOpcP2jIYUUDF9jMicFxqzw6Xc3V85FQS"
      "remember_token" => null
      "created_at" => "2019-12-30 19:41:11"
      "updated_at" => "2019-12-30 19:41:11"
    ]
    #original: array:10 [
      "id" => 1
      "firstName" => "komail"
      "lastName" => "fayazbakhsh"
      "role" => 1
      "email" => "komail.f67@gmail.com"
      "email_verified_at" => null
      "password" => "$2y$10$M5mRU1QkZ84nSkkalAlyVOpcP2jIYUUDF9jMicFxqzw6Xc3V85FQS"
      "remember_token" => null
      "created_at" => "2019-12-30 19:41:11"
      "updated_at" => "2019-12-30 19:41:11"
    ]
    #changes: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #visible: []
    #guarded: array:1 [
      0 => "*"
    ]
    #rememberTokenName: "remember_token"
  }
  1 => App\User {#339
    #fillable: array:5 [
      0 => "firstName"
      1 => "lastName"
      2 => "userName"
      3 => "email"
      4 => "password"
    ]
    #hidden: array:2 [
      0 => "password"
      1 => "remember_token"
    ]
    #casts: array:1 [
      "users_email_verified_at" => "datetime"
    ]
    #connection: "mysql"
    #table: "users"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:10 [
      "id" => 2
      "firstName" => "kasra"
      "lastName" => "karami"
      "role" => 3
      "email" => "kasra@gmail.com"
      "email_verified_at" => null
      "password" => "$2y$10$wSVxxFFF7uWztfP7ZPytaOT.PPUJObnddLdyMWRIKLgfVa7JjZEqu"
      "remember_token" => null
      "created_at" => "2019-12-30 19:42:20"
      "updated_at" => "2019-12-30 19:42:20"
    ]
    #original: array:10 [
      "id" => 2
      "firstName" => "kasra"
      "lastName" => "karami"
      "role" => 3
      "email" => "kasra@gmail.com"
      "email_verified_at" => null
      "password" => "$2y$10$wSVxxFFF7uWztfP7ZPytaOT.PPUJObnddLdyMWRIKLgfVa7JjZEqu"
      "remember_token" => null
      "created_at" => "2019-12-30 19:42:20"
      "updated_at" => "2019-12-30 19:42:20"
    ]
    #changes: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #visible: []
    #guarded: array:1 [
      0 => "*"
    ]
    #rememberTokenName: "remember_token"
  }
  2 => null
  3 => null
  4 => null
  5 => null
  6 => null
  7 => null
  8 => null
]

What is the reason of these null values?

1 Answers1

0

You can use Laravel Accessor instead of this way.

Define an accessor in Project model, Like this:

public function getFullNameAttribute()
{
    return $this->user->firstName + " " + $this->user->lastName;
}

And you can use this attribute like this: $project->full_name;

If you would like these computed values to be added to the array / JSON representations of your model, You should use appends in Project model, For example:

protected $appends = ["full_name"]

public function getFullNameAttribute()
{
    return $this->user->firstName + " " + $this->user->lastName;
}