eloquent - Laravel relationship does not load - Stack Overflow

Here is my Model class with defined relationships. And all of them work besides tax_class(). I have tri

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 This is only a guess without seeing other information, but to the relation, should client_tax instead be tax_client? ... based on the previous relation specified as schedule_client and not client_schedule. – Paul T. Commented Feb 21 at 4:01
Add a comment  | 

1 Answer 1

Reset to default 0

Possible 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);

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170095a4614871.html

相关推荐

  • eloquent - Laravel relationship does not load - Stack Overflow

    Here is my Model class with defined relationships. And all of them work besides tax_class(). I have tri

    2小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信