![](/img/trans.png)
[英]namespacing models - FatalErrorException Call to a member function - Laravel 4
[英]FatalErrorException Call to a member function except() on null
我正在建立一個 QA,當我嘗試提出問題時,我收到了這個錯誤。 FatalErrorException Call to a member function except() on null in web.php line 10
這是 web.php 代碼
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('questions', 'QuestionsController')->except('show');
Route::get('/questions/{slug}', 'QuestionsController@show')->name('questions.show');
在QuestionsController.php
文件中show
function 是
public function show(Question $question)
{
$question->increment('views');
return view('questions.show', compact('question'));
}
另外,這里是顯示問題時的視圖部分
<div class="card">
<div class="card-header">
<div class="d-flex align-items-center">
<h>{{ $question->title }} </h1>
<div class="ml-auto">
<a href="{{ route('questions.index') }}" class="btn btn-outline-secondary">Back to all Questions</a>
</div>
</div>
</div>
<div class="card-body">
{{ !! $question->body_html !! }}
</div>
</div>
所以如果我的問題很清楚,我怎樣才能擺脫這個錯誤? 謝謝您的幫助,先生們!
嘗試以下代碼:
Route::resource('questions', 'QuestionsController', ['except' => ['show']]);
我認為是因為您應該首先使用 find() 或其他東西。 例如,您應該通過 id或 slug 或任何東西找到問題,然后您可以增加所需的字段。 例如:
$question = Question::find(5)->increment('views');
或者
$question = Question::where('url', '=', 'some value')->increment('views');
更正你的路線。 按照這個示例代碼。 這是您可以查看的官方文檔[ https://laravel.com/docs/5.4/controllers#resource-controllers]
Route::resource('questions', 'QuestionsController')->except(['show']);
Route::get('/questions/{slug}', 'QuestionsController@show')->name('questions.show');
首先,您必須更改訂單
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/questions/{slug}', 'QuestionsController@show')->name('questions.show');
Route::resource('questions', 'QuestionsController',['except' => [ 'show' ]]);
當您在單獨的路由中使用類似資源的相同 URI 時,您必須放在資源路由之上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.