管理员用户可正常访问Currency关联,普通用户报Trying to get property of non-object
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:
If this outputs$user = User::find(/* ID of a regular user */); dd($user->currency);null, confirm two things in your database:- Does the
userstable have acurrency_id(or custom foreign key) set for this user? - Is there a corresponding record in the
currenciestable with that ID?
- Does the
Fix:
- Option 1: Assign a default currency when creating regular users (e.g., in your user creation logic, set
currency_idto your default currency's ID). - Option 2: Safely access the property using Laravel's
optional()helper to avoid errors:
Or use the null coalescing operator if you want a fallback value:// Returns the symbol if currency exists, null otherwise optional(Auth::user()->currency)->symbol;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 yourUsermodel:public function currency() { // BelongsTo means this user belongs to a single Currency return $this->belongsTo(Currency::class); }In your
Currencymodel (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 yourUsermodel: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
Usermodel: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

