Kenziiee Flavius
Kenziiee Flavius

Reputation: 1949

Trying to get property of non-object - Laravel 5.1

im trying to create a simple 'active page' method in laravel 5.1. Somehow i've done this before but it just doesn't want to work this time around. I KNOW its something simple because I did it very similar before its annoying me please help!

The Code - PagesController

class PagesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function home()
    {
        $active = [
            'home' => 'active',
            'tasks'=> ''
        ];
        return view('pages.home')->with('active', $active);
        }
    }

Simple enough made an array with two values and on the view i just use this

Homepage

<div class="{{ $active->home }}">Homepage</div>
<div class="{{ $active->tasks }}">Tasks</div>

So thats basically it..

The Error

"Trying to get property of non-object" Not sure why its saying this but i assume its asomething to do with the way i made the array or.. something..? all help and feedback is appreciated!

Upvotes: 1

Views: 1988

Answers (3)

odannyc
odannyc

Reputation: 718

$active is an array, so you need to access it as an array.

For example, to access $active->home , you need to do $active['home']

Upvotes: 1

Joaquin Javi
Joaquin Javi

Reputation: 876

Hi you're passing array inside array , Yo must use dd(your var to see object composition)

return view('pages.home')->with($active);

And then acces {{$home}}

Upvotes: 1

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

Trying to get property of non-object means you're... trying to get property of non-object :) $active is an array, so you need to access its elements as array:

<div class="{{ $active['home'] }}">Homepage</div>
<div class="{{ $active['tasks'] }}">Tasks</div>

Upvotes: 2

Related Questions