mardi 1 novembre 2016

ErrorException in HomeController.php line 27: Trying to get property of non-object

I'm on Laravel 5.3 and I'm using view and I intend to share a variable globally to all of my views and this global variable contents the user's info who is currently logged in.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use View;

//use models
use App\profile;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct()
    {
        $this->middleware('auth');

        View::share('user_info',profile::where('username',Auth::user()->username)->get());

    }
    public function index()
    {
        $current_page = 'home';
        return view('pages.home');
    }
}

but unfortunately it gives me this error

ErrorException in HomeController.php line 27: Trying to get property of non-object

and the line 27 is this line

View::share('user_info',profile::where('username',Auth::user()->username)->first());

it seem's like the problem is 'Auth::user()->username' because If I manually specify the username, it gives me the collection I need.

Any ideas, help please?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire