Here is my Model class with defined relationships. And all of them work besides tax_class(). I have tried to debug many different ways but it just does not work, while others designed exactly the same way do... Is this a known issue and what could be the resolution. when I try to call the relationship it always returns null.
class Client extends Model
{
use HasFactory;
protected $fillable = [
'manager_id',
'email',
'name',
'type',
'tax_class_id',
'state_id',
'city',
'contract_type',
];
protected $casts = [
'tax_class' => TaxClasses::class,
];
public function manager()
{
return $this->belongsTo(User::class, 'manager_id')->withDefault(); // Returns a default manager if none is assigned
}
public function tax_class()
{
return $this->belongsTo(TaxClass::class, 'tax_class_id');
}
public function documents()
{
return $this->hasMany(Document::class);
}
public function state()
{
return $this->belongsTo(State::class);
}
public function schedules()
{
return $this->belongsToMany(Schedule::class, 'schedule_client');
}
public function taxes()
{
return $this->belongsToMany(Tax::class, 'client_tax');
}
}
and here is the TaxClass:
class TaxClass extends Model
{
protected $table = 'tax_classes';
use HasFactory;
protected $fillable = ['name'];
protected $casts = [
'name' => TaxClasses::class
];
public function clients()
{
return $this->hasMany(Client::class);
}
}
Here is my Model class with defined relationships. And all of them work besides tax_class(). I have tried to debug many different ways but it just does not work, while others designed exactly the same way do... Is this a known issue and what could be the resolution. when I try to call the relationship it always returns null.
class Client extends Model
{
use HasFactory;
protected $fillable = [
'manager_id',
'email',
'name',
'type',
'tax_class_id',
'state_id',
'city',
'contract_type',
];
protected $casts = [
'tax_class' => TaxClasses::class,
];
public function manager()
{
return $this->belongsTo(User::class, 'manager_id')->withDefault(); // Returns a default manager if none is assigned
}
public function tax_class()
{
return $this->belongsTo(TaxClass::class, 'tax_class_id');
}
public function documents()
{
return $this->hasMany(Document::class);
}
public function state()
{
return $this->belongsTo(State::class);
}
public function schedules()
{
return $this->belongsToMany(Schedule::class, 'schedule_client');
}
public function taxes()
{
return $this->belongsToMany(Tax::class, 'client_tax');
}
}
and here is the TaxClass:
class TaxClass extends Model
{
protected $table = 'tax_classes';
use HasFactory;
protected $fillable = ['name'];
protected $casts = [
'name' => TaxClasses::class
];
public function clients()
{
return $this->hasMany(Client::class);
}
}
Share
Improve this question
edited Feb 21 at 3:58
Paul T.
4,95212 gold badges47 silver badges63 bronze badges
asked Feb 21 at 2:40
Kristian MartirosyanKristian Martirosyan
131 gold badge1 silver badge5 bronze badges
1
|
1 Answer
Reset to default 0Possible Issues
Method Naming Conflict
- Laravel uses camelCase for relationships. If you name your function with an underscore (_), Laravel might not recognize it properly.
- Example of a problematic relation:
Blockquote
public function user_profile() {
return $this->hasOne(UserProfile::class);
}
Laravel might not detect user_profile correctly. Eloquent Tries to Guess Foreign Key Name
Laravel assumes the foreign key is user_profile_id instead of user_id (if using a custom table).
Solution: Rename the Function Using CamelCase
Instead of using an underscore, use camelCase:
public function userProfile() {
return $this->hasOne(UserProfile::class);
}
client_tax
instead betax_client
? ... based on the previous relation specified asschedule_client
and notclient_schedule
. – Paul T. Commented Feb 21 at 4:01