这是我的家庭控制器:
<?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 | 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)
我现在该怎么办?
答案 0 :(得分:0)
在您的观点中尝试:
<img src="{{ !$profile ?: url('images/'.$profile->logo) }}" alt="Profile-Image" class="img-circle" width="44">
{{ $profile->company_name ?: Auth::user()->username}}