我有这个应用程序,我试图在其中寻找问题的作者,以便将答案标记为最佳答案。
我创建了一个AcceptAnswerController
,它已经在routes/web.php
文件中注册,如下所示:
AcceptAnswerController.php
class AcceptAnswerController extends Controller
{
public function __invoke(Answer $answer)
{
$this->authorize('accept', $answer);
$answer->question->acceptBestAnswer($answer);
return back();
}
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('questions', 'QuestionsController')->except('show');
Route::resource('questions.answers', 'AnswersController')->only(['store', 'edit', 'update', 'destroy']);
Route::get('questions/{slug}', 'QuestionsController@show')->name('questions.show');
Route::post('answers/{answer}/accept', 'AcceptAnswerController')->name('answers.accept');
在“答案”视图中,我具有以下内容:
<div class="row mt-4">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="card-title">
<h2>{{ $answersCount . " " . str_plural('Answer', $answersCount) }}</h2>
</div>
<hr>
@include ('layouts._messages')
@foreach ($answers as $answer)
<div class="media">
<div class="d-fex flex-column vote-controls">
<a title="This answer is useful" class="vote-up">
<i class="fas fa-caret-up fa-3x"></i>
</a>
<span class="votes-count">1230</span>
<a title="This answer is not useful" class="vote-down off">
<i class="fas fa-caret-down fa-3x"></i>
</a>
@can ('accept', $answer)
<a title="Mark this answer as best answer"
class="{{ $answer->status }} mt-2"
onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
>
<i class="fas fa-check fa-2x"></i>
</a>
<form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
@csrf
</form>
@else
@if ($answer->is_best)
<a title="The question owner accepted this answer as best answer"
class="{{ $answer->status }} mt-2"
>
<i class="fas fa-check fa-2x"></i>
</a>
@endif
@endcan
</div>
<div class="media-body">
{!! $answer->body_html !!}
<div class="row">
<div class="col-4">
<div class="ml-auto">
@can ('update', $answer)
<a href="{{ route('questions.answers.edit', ['question' => $question->id, 'answer' => $answer->id]) }}" class="btn btn-sm btn-outline-info">Edit</a>
@endcan
@can ('delete', $answer)
<form class="form-delete" method="post" action="{{ route('questions.answers.destroy', [$question->id, $answer->id]) }}">
@method('DELETE')
@csrf
<button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')">Delete</button>
</form>
@endcan
</div>
</div>
<div class="col-4"></div>
<div class="col-4">
<span class="text-muted">Answered {{ $answer->created_date }}</span>
<div class="media mt-2">
<a href="{{ $answer->user->url }}" class="pr-2">
<img src="{{ $answer->user->avatar }}">
</a>
<div class="media-body mt-1">
<a href="{{ $answer->user->url }}">{{ $answer->user->name }}</a>
</div>
</div>
</div>
</div>
</div>
</div>
<hr>
@endforeach
</div>
</div>
</div>
</div>
问题的作者单击检查图标后,应将答案标记为最佳答案,但出现以下错误:
抱歉,找不到您要查找的页面
我看到URL中缺少答案ID,如下所示:
实际上应该是这样的:
我似乎无法弄清楚为什么我在表单操作中将它作为路由参数传递给了我。如果用户试图编辑答案,也会发生同样的事情。
答案 0 :(得分:1)
我可以建议您在这里稍微调整一下策略,而不必提交表单吗?
如果您将POST换成GET请求,这实际上很简单。
Answers.blade.php
@foreach( $answers as $answer )
@can('accept', $answer)
<a href="answers/{{ $answer->id }}/accept" title="Mark this answer as best answer" class="{{ $answer->status }} mt-2">
<i class="fas fa-check fa-2x"></i>
</a>
@else
<p>Do your thing</p>
@endcan
@endforeach
routes.web.php
Route::get('answers/{answer}/accept', 'AcceptAnswerController');
答案 1 :(得分:0)
因此,在更仔细地检查了我的代码之后,控制器,路由和视图的其他一切似乎都还可以。我能够将问题隔离到这个区域
@can ('accept', $answer)
<a title="Mark this answer as best answer"
class="{{ $answer->status }} mt-2"
onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
>
<i class="fas fa-check fa-2x"></i>
</a>
<form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
@csrf
</form>
@endcan
我是certian,$answer->id
应该返回正确的ID,但是我对$answer->status
不太确定,因此我决定检查在Answer模型中定义的访问器。
public function getStatusAttribute()
{
return $this->isBest() ? 'vote-accepted' : '';
}
public function isBest()
{
return $this->id = $this->question->best_answer_id; /** here is the problem **/
}
问题出在我身上。上面的isBest方法应该返回一个布尔值,但我误分配了。这是简单的解决方法。
public function isBest()
{
return $this->id === $this->question->best_answer_id; /** here is the problem **/
}