2016-03-21 74 views
0

我有一個數據庫設計類似下面的模型關係和傳遞對象

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關聯?

感謝

回答

1

我敢打賭,這是因爲這行:

Route::resource('/', 'PollResponseController', ['except' => ['create', 'show', 'edit', 'update', 'destroy']]); 

的第一個參數預計資源法的預計資源名稱(即「民意調查」或「polls.response」)。

我不知道你想實現這個電話有什麼,但如果你想註冊一家商店的路線,以「/存儲」你唯一的辦法是定義一個像這樣的定期航線:

Route::post('store/{polls}', '[email protected]'); 

也就是說因爲laravel的寧靜資源必須具備某種基本路徑,所以不能將它們放入域名的根目錄(example.com/{id},example.com/{id}/edit,example.com/store/ {ID}等)

另一種方法是提供資源名稱這樣的:

Route::resource('polls.response', 'PollResponseController', ['except' => ['create', 'show', 'edit', 'update', 'destroy']]); 

但是,您必須相應地更改您的表單網址。

我還檢查了這行:

{!! Form::model(new App\PollResponse, ['route' => ['store', $poll->id]]) !!} 

假設「polls.response」被註冊的資源,您可能需要通過「polls.response.store」爲路徑名。