第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel ModelNotFoundException:沒有關(guān)于模型[]的查詢結(jié)果

Laravel ModelNotFoundException:沒有關(guān)于模型[]的查詢結(jié)果

PHP
慕桂英3389331 2021-05-04 10:22:03
運(yùn)行測試時出現(xiàn)此錯誤:ModelNotFoundException:沒有針對模型[App \ Content]的查詢結(jié)果這是我的測試:/** @test */public function it_loads_homepage(){    $this->withoutExceptionHandling();    $response = $this->get('/');    $response->assertStatus(200);}這是我的控制器:public function home(){    $topbar = Content::where('slug', 'topbar')->firstOrFail();    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')    ]);}這是我的路線文件:Route::get('/', 'PageController@home');Route::post('/admin/content', 'ContentsController@store');Auth::routes();Route::get('/home', 'HomeController@index')->name('home');Route::middleware('auth')->prefix('admin')->group(function () {    Route::get('/dashboard', 'DashboardsController@index');    Route::patch('/contents/{content}', 'ContentsController@update');});如果有人對如何幫助有任何想法,將不勝感激,謝謝!附注:如果需要添加其他任何內(nèi)容,請告訴我,謝謝!
查看完整描述

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ū)分大小寫的)。您在評論之一中說您確定,因此請使用TinkerArtisan 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();


查看完整回答
反對 回復(fù) 2021-05-21
?
汪汪一只貓

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')

]);


查看完整回答
反對 回復(fù) 2021-05-21
  • 2 回答
  • 0 關(guān)注
  • 185 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號