5

So I changed the name of a column in my Laravel app and the party started..

The change was from name_id to seller_id

Before in view:

$transaction->user->first_name

Before in controller Transaction:

class Transaction extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }
 }

After and before in controller calling the view:

public function getInfoUser($name){

   $user = User::where('register_id', $name)->where('id', auth()->user()->id)->first();

   if($user){
   return view('users.user', compact('user'));
   }
}

After in view:

$transaction->seller->first_name

After in controller Transaction:

class Transaction extends Model
{
    protected $primaryKey = 'seller_id';

    public function user(){
        return $this->belongsTo('App\User');
    }
}

After returns:

Trying to get property of non-object (View: /Users/tronne/Documents/web/resources/views/users/user.blade.php) in c7128907204dffe6676c7d88cbbc47.php (line 108) 
at CompilerEngine->handleViewException(object(ErrorException), 0) in
PhpEngine.php (line 45)
at PhpEngine-evaluatePath('/Users/tronne/Documents/web/storage/framework/views/c7128907204dffe6676c7d88cbbc47.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'user' => object(User)))
in CompilerEngine.php (line 59)
at CompilerEngine-get('/Users/tronne/Documents/web/resources/views/users/user.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'user' => object(User)))
in View.php (line 137)

For reference the table users has the standard column name "user_id" and "first_name" and the table transactions (transactions controller) now has "seller_id"

Not sure if it affects, but in the SQL table, the primary keys are in both cases "id" autoincrement

What I'm doing wrong?

8
  • please add the full error Commented Jan 31, 2018 at 10:23
  • protected $table = "users"; @Sohel0415 the error its quiet obvious. Commented Jan 31, 2018 at 10:25
  • @LeoinstanceofKelmendi I was trying to understand no-object of what Commented Jan 31, 2018 at 10:27
  • @LeoinstanceofKelmendi I'm in the transactions model, protected $table = "users"; should go to users model right? Here it's assuming correctly that the table name is transactionS Commented Jan 31, 2018 at 10:29
  • please add full error and your controller code and your model that others can understand clearly Commented Jan 31, 2018 at 10:38

1 Answer 1

1

You have a user relationship in your Transaction Model but you're trying to access it as seller in your view.

Change:

$transaction->seller->first_name

To:

$transaction->user->first_name

Or

You could also change the relationship in your model to seller. If that's what you need.

Finally:

Display data in the view only if there are records in the relationship.

@if(!empty($transaction->user))
        $transaction->user->first_name
@else
        //No users
@endif
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the answer! upvoted but same thing.. same error, then there're 2 errors, 1 less ;)
@TrOnNe You still ge the same error? Please post the full error message
added, the error is pointing to the line $transaction->seller->first_name
@TrOnNe The error occurs when there are no users belonging to the relationship. Are you use that particular transaction has users?
It solves the error but not the problem haha, $transaction->user is the user itself who made the transaction, so is not empty, the problem is that laravel is not finding the $transaction->user because of some problem with the column name
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.