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

时间:2016-08-18 20:55:06

标签: php laravel laravel-5 laravel-5.2 laravel-5.1

这是我的家庭控制器:

<?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}}

这是我的个人资料表:

user_id | company_name |标志

2 | deneme | zoro.jpg

如你所见,我没有user_id = 1的数据所以$ profile值不是来自db而且我得到了这个错误:

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)

我现在该怎么办?

1 个答案:

答案 0 :(得分:0)

在您的观点中尝试:

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