Laravel新手遇Trying to get property of non-object错误求助
Hey there, let's break down this error you're hitting and fix it step by step!
The Root Cause
That Trying to get property of non-object error happens because you're trying to access a property (is_complete) on a value that's not an object—most likely null. Looking at your ProfileController@index code:
$current_transaction = $item->profile->current_transaction; if(!$current_transaction->is_complete){ // ... flash message and redirect }
Even though you check that $item->profile exists, there's no guarantee that $item->profile->current_transaction returns a valid Transaction object. If the user has no associated current transaction, this will be null, and calling ->is_complete on null throws the error you're seeing.
Fixes to Implement
1. Add a Check for current_transaction Before Accessing Its Properties
Update your controller logic to verify that current_transaction exists first:
public function index() { $item = auth()->user(); if(!$item->profile){ return redirect()->to('/dashboard'); } $current_transaction = $item->profile->current_transaction; // Only run the check if current_transaction actually exists if($current_transaction && !$current_transaction->is_complete){ flash('You can not update your profile while you still have an incomplete transaction.')->error(); return redirect()->to('/dashboard'); } return view('profiles.index',compact('item')); }
If you're using Laravel 8 or newer, you can also use optional property chaining to make this cleaner (though you still need to adjust the logic to avoid false positives):
// Check if a current transaction exists AND it's not complete if($item->profile->current_transaction?->is_complete === false){ flash('You can not update your profile while you still have an incomplete transaction.')->error(); return redirect()->to('/dashboard'); }
2. Verify Your Eloquent Relationship is Correct
Double-check your Profile model to make sure the current_transaction relationship is defined properly. For example, if a Profile has one current Transaction, it should look like this:
// app/Models/Profile.php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Profile extends Model { // ... other model code public function current_transaction() { // Adjust the model name and foreign key to match your database structure return $this->hasOne(Transaction::class); } }
If the relationship method is misnamed (e.g., using camelCase currentTransaction but calling it with snake_case in the controller) or the foreign key is incorrect, Eloquent will return null for the relationship, triggering the same error.
3. Optional: Make Your Code More Robust
For extra safety, you can use Laravel's optional() helper to avoid errors if any part of the relationship chain is null:
$currentTransaction = optional(optional($item)->profile)->current_transaction;
But in your case, since you already check $item->profile exists, this is just a bonus layer of protection.
内容的提问来源于stack exchange,提问作者Nnamdi

