user608207
user608207

Reputation: 39

Laravel issue- "Trying to get property of non-object"

Controller :

$args = array();
$args['name'] = "Robin";
$args['email'] = "[email protected]";
$clientpayments = Payments::getPaymentByClient($id);
$args['activity'] = $clientpayments;
return view('clients.show',["args" => $args]); 

View:

{{ $args->name }}
{{ $args->email }}

@if (isset($args['activity']))        
@foreach ($args['activity'] as $act)
{{$act->job_name}}
@endforeach
@endif;

So what the issue is is that $activity loop works fine but the $name and $email is returning a non-object error... Any ideas to where I'm going wrong?

Thanks!

Upvotes: 1

Views: 178

Answers (3)

user608207
user608207

Reputation: 39

Got it.

A silly mistake, but I'm just learning Laravel. I was including $args in the View rather than just $name, $email and $activity which worked perfectly.

Thanks anyway.

Upvotes: 0

E. S.
E. S.

Reputation: 2919

You are trying to access an object value, but you are sending an array to your view.

$payments = Payments::getPaymentByClient($id);

$args = array([
  'name' => 'Robin',
  'email' => '[email protected]',
  'activity' => $payments, // Expecting a collection
]);

return view('clients.show', [
  "args" => (object) $args // Cast into an object
]); 

Blade template (if you have an object)

{{ $args->name }}
{{ $args->email }}

// If your activity is a collection
@foreach ($args->activity as $act)
  {{ $act->job_name }}
@endforeach

Blade template (if you have an array)

{{ $args['name'] }}
{{ $args['email'] }}

// If your activity is a collection
@foreach ($args['activity'] as $act)
  {{ $act->job_name }}
@endforeach

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

Since you're using an array, change this:

{{ $args->name }}
{{ $args->email }}

To:

{{ $args['name'] }}
{{ $args['email'] }}

Upvotes: 1

Related Questions