mostafalaravel
2 weeks ago

User : findOrFail return first_name and last_name in attributes but $user->first_name is empty

Posted 2 weeks ago by mostafalaravel

Hello ,

With this code,

$user = User::findOrFail($id);
        dd($user);

you can see the first_name and last_name attributes

wasRecentlyCreated: false
  #attributes: array:34 [
    "id" => 603
    "name" => "none"
    "email" => "[email protected]"
    "email_verified_at" => null
    "password" => "y$heVEHGJ4w5Q.8Fcdi/p1LORM0l5A22/1XUa.nUppYiyDrcuFvpqcC"
    "verified" => "0"
    "verified_token" => "AT4FPjnU3iPqg0bYeTnj6M0PT7LvchcoITlJAHDe"
    "admin" => "0"
    "remember_token" => null
    "created_at" => "2019-08-26 10:34:45"
    "updated_at" => "2019-08-26 10:36:50"
    "first_name" => "xyz"
    "last_name" => "abc"
    "telephone" => "0323165487"
    "job_title" => null
    "abbreviation" => null
    "start_date" => null
    "stop_date" => null
    "birth_date" => null
    "street" => null
    "city" => null
    "post_code" => null
    "country" => "FR"
    "deleted_at" => null
    "password_update_count" => 2
    "password_last_update_date" => "2019-08-26"
    "logout" => true
    "password_updated_by" => "admin"
    "teamleader" => null
    "planner" => null
    "language" => null
    "is_internal" => false
    "report_department" => null
    "avatar" => "user.jpg"
  ]

BUT : when I ´dd($user->first_name)´ it's empty !

Here the user model :

<?php

namespace App;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Spatie\Activitylog\Traits\LogsActivity;

class User extends Authenticatable implements MustVerifyEmail
{
    use SoftDeletes;
    use Notifiable;
    use LogsActivity;
    const VERIFIED_USER = '1';
    const UNVERIFIED_USER = '0';

    const ADMIN_USER = '1';
    const REGULAR_USER = '0';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        "name",
        "email",
        "password",
        "verified",
        "verified_token",
        "admin",
        "abbreviation",
        "avatar",
        "country",
        "email",
        "first_name",
        "is_internal",
        "job_title",
        "language",
        "last_name",
        "planner",
        "telephone",
        "teamleader",
        "start_date",
        "stop_date",
        "report_department"
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function groups()
    {
        return $this->belongsToMany(Group::class);
    }

    public function isVerified()
    {
        return $this->verified == User::VERIFIED_USER;
    }

    public function isAdmin()
    {
        return $this->admin == User::ADMIN_USER;
    }

    public static function generateVerificationCode()
    {
        return Str::random(40);
    }

    public function logs()
    {
        return $this->hasMany(Log::class, 'causer_id');
    }

    public function sdinfo()
    {
        return $this->hasOne(Sdinfo::class);
    }

    public function scopeMembers($query)
    {

        return $query->has('groups');
    }


    public static function boot()
    {
        static::creating(function ($user) {
            //$fullName = explode('.',strstr($user->email, '@', true));
            $user->first_name = "";
            $user->last_name = "";
        });

        parent::boot();
    }


}

Thanks

Please sign in or create an account to participate in this conversation.