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]);
}
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community