laravel : 表单模型绑定得到 null 所以出错

laravel : form model binding get null so got error

这是我的家庭控制器:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;

use App\Http\Models\Profile as Profile;
use Auth;

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

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $profile = Profile::where('user_id',Auth::user()->id)->first();
        return view('home.index')->with("profile", $profile);
    }
}

这是我的观点(部分):

<img src="{{ $profile ?: url('images/'.$profile->logo) }}" alt="Profile-Image" class="img-circle" width="44">
{{ $profile->company_name ?: Auth::user()->username}}

这是我的个人资料 table :

user_id | company_name |标志

2 |定义 | zoro.jpg

如您所见,我没有包含 user_id = 1 的数据,因此 $profile 值不是来自数据库,而且我收到此错误:

Trying to get property of non-object
(View: /opt/lampp/htdocs/works/menu/resources/views/layout/header.blade.php)
(View: /opt/lampp/htdocs/works/menu/resources/views/layout/header.blade.php)
(View: /opt/lampp/htdocs/works/menu/resources/views/layout/header.blade.php)

我现在可以做什么?

在您的视图中尝试:

<img src="{{ !$profile ?: url('images/'.$profile->logo) }}" alt="Profile-Image" class="img-circle" width="44">
{{ $profile->company_name ?: Auth::user()->username}}