我有一個數據庫設計類似下面的模型關係和傳遞對象
Poll
id | name
--------------
1 | Poll One
PollQuestion
id | poll_id | question
-------------------------
1 | 1 | Something
PollAnswer
id | question_id | answer
-------------------------------
1 | 1 | Answer 1
-------------------------------
2 | 1 | Answer 2
-------------------------------
3 | 1 | Answer 3
一項民意調查可以有一個PollQuestion和PollQuestion可以有很多PollAnswer的。
這一切工作正常,我可以創建民意調查,問題和答案。這些都是在後臺完成的,這需要您以管理員用戶身份登錄。
在前端,我需要顯示投票問題和可能的答案。我建立了一個PollResponse模型,Poll可以有很多PollResponse。模型是這個樣子
class Poll extends Model
{
public function pollQuestion()
{
return $this->hasOne('App\PollQuestion', 'poll_id');
}
public function pollResponse()
{
return $this->hasMany('App\PollResponse', 'poll_id');
}
}
class PollResponse extends Model
{
public function poll()
{
return $this->belongsTo('App\Poll');
}
}
然後我有我的路線設置這樣下面
Route::group(['middleware' => ['web']], function() {
Route::model('polls.response', 'PollResponse');
Route::resource('/', 'PollResponseController', ['except' => ['create', 'show', 'edit', 'update', 'destroy']]);
Route::group(['middleware' => ['admin_logged', 'can_see']], function()
{
Route::model('polls', 'Poll');
Route::bind('polls', function($value, $route) {
return App\Poll::whereId($value)->first();
});
Route::resource('admin/polls', 'PollController');
Route::model('polls.questions', 'PollQuestion');
Route::resource('polls.questions', 'PollQuestionController', ['except' => ['index', 'create', 'show', 'edit', 'destroy']]);
});
});
這樣的想法是,普通用戶可以訪問主域名,看到了最新的民意調查問題的可能的答案。然後他們選擇 的答案並提交他們的回覆。對此的看法如下
<div class="panel-heading">
<h3 class="panel-title"> {{ $question->question }}</h3>
</div>
<div class="panel-body">
{!! Form::model(new App\PollResponse, ['route' => ['store', $poll->id]]) !!}
@if (count($answers) !== 0)
@foreach($answers as $key => $value)
<div class="radio">
<label>
<input type="radio" name="optionsRadios" value="{{ $value->answer }}"> {{ $value->answer }}
</label>
</div>
@endforeach
@endif
{!! Form::submit('Save', array("class"=>"btn btn-info pull-right ")) !!}
{!! Form::close() !!}
</div>
所以這一切工作正常。儘管在我的PollResponseController中,但我的商店功能有問題。如果我提交響應 ,則將poll_id添加到路由中定義的URL中。但是在存儲功能,我目前在做
public function store(Request $request, Poll $poll)
{
dd($poll);
}
我希望它可以輸出目前的投票對象,但它是目前輸出一個空的投票對象。如何將相關的Poll Object引入此函數中,以便將響應與特定的Poll關聯?
感謝