Reputation: 354
I get this error when running my test:
ModelNotFoundException: No query results for model [App\Content]
Here is my test:
/** @test */
public function it_loads_homepage()
{
$this->withoutExceptionHandling();
$response = $this->get('/');
$response->assertStatus(200);
}
Here is my Controller:
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')
]);
}
Here is my routes file:
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');
});
If anyone has any idea on how to help that would be much appreciated, thank you!
P.S. If I need to include anything else please let me know Thank you!
Upvotes: 2
Views: 7926
Reputation: 14271
The exception tells you that there isn't a Content
object that matches the condition. From the docs:
Not Found Exceptions
Sometimes you may wish to throw an exception if a model is not found. This is particularly useful in routes or controllers. The
findOrFail
andfirstOrFail
methods will retrieve the first result of the query; however, if no result is found, aIlluminate\Database\Eloquent\ModelNotFoundException
will be thrown.
Check out your where
condition:
$topbar = Content::where('slug', 'topbar')->firstOrFail();
Make sure you have a record in your contents
(?) table with the slug=topbar
(remember this is case-sensitive). You said in one of your comments that you are sure, so check it in the Artisan Console using Tinker:
$ php artisan tinker
> Content::where('slug','topbar')->count();
This should output:
[!] Aliasing 'Usage' to 'App\Content' for this Tinker session.
=> 1 // or maybe more..
To get your records you can try this instead (in your controller):
$topbar = Content::where('slug', 'LIKE', '%topbar%')->firstOrFail();
Also, its very likely that your sentence is just a sample of your current code, but if not, make sure to pass the actual value in your where clause:
$topbar = Content::where('slug', 'LIKE', request('slug'))->firstOrFail();
Upvotes: 1
Reputation: 35
The find() method from query builder can return a Model instance or null if no record was found in database. So you have to handle this by checking first if user exists. If he does you can for example show his avatar, otherwise you can show login button.
Your code is a bit messy and I would advice you to not mix logic with view code. You should get user in controller then pass him to view like
$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')
]);
Upvotes: 1