Support the ongoing development of Laravel.io →
posted 9 years ago
IOC

Hello, I am trying to inject an attribute on my construtor to avoid to redeclare it on every method so this is my method but's when I am not logged, it fail with Fatal error Non object...

class ModController extends Controller {

private $blog;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');

    $this->blog= Auth::user()->blogs()->first(); //this work only when I am logged
}

public function dashboard()
{
    // here I want to avoid to make on every method this line
    // $blog =  Auth::user()->blogs()->first();
    return view('panel.dashboard', ['blog' => $this->blog]);
}
Last updated 2 years ago.
0

If i understand your question correctly, you have a variable $blog which always needs to be available within panel.dashboard view.

Have a look at http://laravel.com/docs/5.1/views#view-composers

0

Sign in to participate in this thread!

Eventy

Your banner here too?

LonnyX lonnyx Joined 30 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.