2 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個贊
異常告訴您沒有Content
與條件匹配的對象。從文檔:
找不到例外
有時,如果找不到模型,您可能希望拋出異常。這在路由或控制器中特別有用。該
findOrFail
和firstOrFail
方法將檢索查詢的第一個結(jié)果; 但是,如果未找到結(jié)果,Illuminate\Database\Eloquent\ModelNotFoundException
將拋出a。
檢查您的where
狀況:
$topbar = Content::where('slug', 'topbar')->firstOrFail();
確保您在contents
(?)表中有一條記錄slug=topbar
(請記住,這是區(qū)分大小寫的)。您在評論之一中說您確定,因此請使用Tinker在Artisan Console中進(jìn)行檢查:
$ php artisan tinker> Content::where('slug','topbar')- >count();
這應(yīng)該輸出:
[!]在此Tinker會話中,將“用法”別名為“ App \ Content”。
=> 1 //或更多。
要獲取記錄,您可以嘗試改用此方法(在您的控制器中):
$topbar = Content::where('slug', 'LIKE', '%topbar%')->firstOrFail();
同樣,您的句子很可能只是您當(dāng)前代碼的示例,但如果不是,請確保在where子句中傳遞實(shí)際值:
$topbar = Content::where('slug', 'LIKE', request('slug'))->firstOrFail();

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個贊
查詢構(gòu)建器的find()方法可以返回Model實(shí)例;如果在數(shù)據(jù)庫中未找到任何記錄,則返回null。因此,您必須通過首先檢查用戶是否存在來處理此問題。如果他這樣做,則可以顯示他的頭像,否則,可以顯示登錄按鈕。
您的代碼有點(diǎn)混亂,我建議您不要將邏輯與視圖代碼混在一起。您應(yīng)該讓用戶進(jìn)入控制器,然后再將其傳遞給查看者
$topbar = Content::where('slug', 'topbar')->get()->toArray();
return view('welcome')->with([
'logo' => Arr::get($topbar->content, 'logo', asset('images/logo.png')),
'quote' => Arr::get($topbar->content, 'quote'),
'quote_author' => Arr::get($topbar->content, 'quote_author')
]);
- 2 回答
- 0 關(guān)注
- 185 瀏覽
添加回答
舉報