You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

管理员用户可正常访问Currency关联,普通用户报Trying to get property of non-object

Troubleshooting "Trying to get property of non-object" for Regular Users' Currency Association

Hey there, let's break down why your admin users can access the Currency association just fine, but regular users are hitting that frustrating "Trying to get property of non-object" error. Here are the most common causes and fixes:

1. Regular Users Don't Have a Linked Currency Record

This is the most likely culprit. Admins probably have a corresponding entry in the currencies table linked to their user record, but regular users don't. When you try to access Auth::user()->currency->some_property, if currency returns null (because no linked record exists), accessing any property on it will throw that error.

How to Check:

  • Fire up Laravel Tinker and run:
    $user = User::find(/* ID of a regular user */);
    dd($user->currency);
    
    If this outputs null, confirm two things in your database:
    • Does the users table have a currency_id (or custom foreign key) set for this user?
    • Is there a corresponding record in the currencies table with that ID?

Fix:

  • Option 1: Assign a default currency when creating regular users (e.g., in your user creation logic, set currency_id to your default currency's ID).
  • Option 2: Safely access the property using Laravel's optional() helper to avoid errors:
    // Returns the symbol if currency exists, null otherwise
    optional(Auth::user()->currency)->symbol;
    
    Or use the null coalescing operator if you want a fallback value:
    Auth::user()->currency?->symbol ?? 'USD';
    

2. Incorrect Association Definition

Double-check that your User and Currency model associations are set up correctly—mixing up belongsTo and hasOne is a common mistake.

Example Correct Setups:

  • If users have one currency (users table has currency_id):
    In your User model:

    public function currency()
    {
        // BelongsTo means this user belongs to a single Currency
        return $this->belongsTo(Currency::class);
    }
    

    In your Currency model (if needed):

    public function users()
    {
        // A currency can belong to many users
        return $this->hasMany(User::class);
    }
    
  • If a currency belongs to one user (currencies table has user_id):
    In your User model:

    public function currency()
    {
        // HasOne means this user has one Currency
        return $this->hasOne(Currency::class);
    }
    

    If your foreign key isn't using Laravel's default naming convention (e.g., custom_currency_id), specify it explicitly:

    return $this->belongsTo(Currency::class, 'custom_currency_id');
    

3. Global Scopes Restricting Currency Access

If you've added a global scope to the Currency model that filters results only for admin users, regular users won't be able to retrieve their currency records (resulting in null).

How to Check:

Look for a boot() method in your Currency model that adds a scope like:

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('admin-only', function ($query) {
        $query->where('user_type', 'admin'); // Example restrictive scope
    });
}

Fix:

  • Remove the restrictive global scope if it's not needed.
  • Or exclude the scope when defining the association in the User model:
    public function currency()
    {
        return $this->belongsTo(Currency::class)->withoutGlobalScopes();
    }
    

If none of these fix the issue, feel free to share the exact association code from your User and Currency models, and we can dig deeper!

内容的提问来源于stack exchange,提问作者Darius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:31:45