[英]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}}
這是我的個人資料表:
2 | 代名詞 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)
我現在能做什么 ?
在您看來,嘗試一下:
<img src="{{ !$profile ?: url('images/'.$profile->logo) }}" alt="Profile-Image" class="img-circle" width="44">
{{ $profile->company_name ?: Auth::user()->username}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.