I want to make an eloquent model out of two table, pekerjaan and user. User hasMany pekerjaan and pekerjaan belongsTo User. How do I add eloquent’s method to the User class in Laravel? I have an error that shows:
Cannot declare class AppModelsUser, because the name is already in use
Here is my User.php model code:
<?php namespace AppModels; use IlluminateContractsAuthMustVerifyEmail; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateFoundationAuthUser as Authenticatable; use IlluminateNotificationsNotifiable; use LaravelFortifyTwoFactorAuthenticatable; use LaravelJetstreamHasProfilePhoto; use LaravelSanctumHasApiTokens; use IlluminateDatabaseEloquentModel as Eloquent; class User extends Eloquent{ public function pekerjaans(){ return $this->hasMany(Pekerjaan::class); } } class User extends Authenticatable { use HasApiTokens; use HasFactory; use HasProfilePhoto; use Notifiable; use TwoFactorAuthenticatable; /** * The attributes that are mass assignable. * * @var string[] */ protected $fillable = [ 'name', 'email', 'username', 'satker', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'password', 'remember_token', 'two_factor_recovery_codes', 'two_factor_secret', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = [ 'profile_photo_url', ]; }
And here is my Pekerjaan.php model:
<?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel as Eloquent; class Pekerjaan extends Eloquent { use HasFactory; protected $guarded = []; public function penyedia(){ return $this->belongsTo(Penyedia::class, 'penyedia_id'); } public function user(){ return $this->belongsTo(User::class, 'user_id'); } }
Thank you
Advertisement
Answer
The error message says it what is the problem:
Cannot declare class AppModelsUser, because the name is already in use
Your problem is you have 2 classes of User
in your User.php
. Remove the first User
class put the function pekerjaans
to the 2nd User
class
<?php namespace AppModels; use IlluminateContractsAuthMustVerifyEmail; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateFoundationAuthUser as Authenticatable; use IlluminateNotificationsNotifiable; use LaravelFortifyTwoFactorAuthenticatable; use LaravelJetstreamHasProfilePhoto; use LaravelSanctumHasApiTokens; use IlluminateDatabaseEloquentModel as Eloquent; class User extends Authenticatable { use HasApiTokens; use HasFactory; use HasProfilePhoto; use Notifiable; use TwoFactorAuthenticatable; /** * The attributes that are mass assignable. * * @var string[] */ protected $fillable = [ 'name', 'email', 'username', 'satker', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'password', 'remember_token', 'two_factor_recovery_codes', 'two_factor_secret', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = [ 'profile_photo_url', ]; public function pekerjaans(){ return $this->hasMany(Pekerjaan::class); } }