Why not $user->first_name ?
Laravel livewire profile show throws an error undefined method method firstName() in user models
I have a table user with column first_name and last_name. I am trying to make an update profile page but I have this error [Call to undefined method App\Models\User::firstName()] I did not name anything firstName but I did name it first_name and last_name. I have searched the entire codebase for any firstName usage but I nothing to be found except the error thrown. I do have an accessor and mutator first_name and last_name. How do solve this problem please thank you.
Model of the user below
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Fortify\TwoFactorAuthenticatable; use Laravel\Jetstream\HasProfilePhoto; use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable { use HasApiTokens; use HasFactory; use HasProfilePhoto; use Notifiable; use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'active',
'admin',
'phone',
'password',
'address'
];
/** Relationship between models ...*/
public function bookings(){
return $this->hasMany(Booking::class);//a user has many bookings
}
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
'created_at',
'updated_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* @var array<int, string>
*/
/**
* Accessors and mutators (method name is the attribute name)
* get: transform the attribute after it has retrieved from database
* set: transform the attribute before it is sent to database
*/
protected function first_name(): Attribute
{
return Attribute::make(
get: fn($value) => ucwords($value), // accessor
set: fn($value) => strtolower($value) // mutator
);
}
protected function last_name(): Attribute
{
return Attribute::make(
get: fn($value) => ucwords($value), // accessor
set: fn($value) => strtolower($value) // mutator
);
}
protected $appends = [
'profile_photo_url',
];
}
Migration of user below
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('first_name'); $table->string('last_name'); $table->string('email')->unique(); $table->boolean('active')->default(true); $table->boolean('admin')->default(false); $table->string('phone', 10); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->string('address'); $table->string('profile_photo_path', 2048)->nullable(); $table->boolean('registered'); $table->rememberToken(); $table->timestamps(); }); // Insert some users (inside the up-function!) DB::table('users')->insert( [ [ 'first_name' => "Peter", 'last_name'=>"Pan", 'email' => "[email protected]", 'active' => true, 'admin' =>true, 'phone'=>'##########', 'email_verified_at' => now(), 'password' => Hash::make('admin1234'), 'address'=>"K#########l", 'profile_photo_path' =>null, 'registered'=>1, 'created_at' => now(),
],
[
'first_name' => "Jane",
'last_name'=>"Doe",
'email' => "[email protected]",
'active' => true,
'admin' =>false,
'phone'=>'0439134521',
'email_verified_at' => now(),
'password' => Hash::make('user1234'),
'address'=>"Mazon City 2939",
'profile_photo_path' =>null,
'registered'=>0,
'created_at' => now(),
]
]
);
They need to be camelCase
protected function firstName(): Attribute
{
It's in the docs https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor
Please or to participate in this conversation.