2017-05-22 10 views
0

私の見解では、tests.indexはデータベースからデータを取得するselectドロップダウンをクリックして私のルートtests.showを開くことができますか?open route .show onchangeをドロップダウンしたとき

マイコントローラ:

public function index(Request $request) 
{ 
    $tests = DB::table('test')->distinct()->get(); 
    return view('tests.index')->with('tests', $tests); 
} 

public function show($id) 
{ 
    $test = Test::findOrFail($id); 
    return view('tests.show', compact('test')); 
} 

マイビューインデックス:

<select id="apports" type="dropdown-toggle" class="form-control" name="apports" onchange="{{ route("tests.show", $test->id) }}"> 
<option value="choisir" selected disabled>Choisir</option> 
@foreach($tests as $test) 
<option class="apports" value="{{$test->id}}">{{$test->apports}}</option> 
@endforeach 

私はここに私のルートを置く:onchange私はエラー "未定義の変数を:テスト" を取得 I次のような選択項目の下にボタンを追加してください:

<a class="btn btn-small btn-success" href="{{ route("tests.show", $test->id) }}">Show</a> 
私は常にテーブルの最後のIDを取得

...

私は、私が選択して値を変更するだけの場合は、ボタンを持っているtests.showを返さないことを好むだろう...誰かが私を助けてください可能性です? $test変数は、インデックス・ビューに設定されていない

+0

'$ test'変数は唯一のビューの上にループ – xmhafiz

+0

foreach'' @内部で呼び出すことができますされ、 '{{ddは($テストは)}}'これを書いて、私 –

+0

をして表示結果を共有はいしかし私は

答えて

0

選択肢の変更を読んでリダイレクトするためのjavascriptスニペットが必要です。

<select id="apports" type="dropdown-toggle" class="form-control" name="apports"> 
    <option value="choisir" selected disabled>Choisir</option> 
    @foreach($tests as $test) 
     <option class="apports" value="{{ route("tests.show", $test->id) }}">{{ $test->apports }}</option> 
    @endforeach 
</select> 

<script> 
    $('select').on('change', function (e) { 
     var link = $("option:selected", this).val(); 
     if (link) { 
      location.href = link; 
     } 
    }); 
</script> 
+0

ありがとうございました!できます ! – ginette

0

あなたはそれでビューをロードするので、唯一の$testsコレクションが提供されています:

return view('tests.index')->with('tests', $tests); 

あなたが探しているものを達成するために、いくつかのJavaScriptを使用する必要があります。あなたのJavaScriptファイルで次に

<select id="apports" type="dropdown-toggle" class="form-control" name="apports" onchange="goToTestPage(this.value)"> 

function goToTestPage(id) { 
    window.location.href = "link to your show route/"+id; 
} 

テストIDがユーザによって動的に変更されている場合はPHPが前に実行されているので、あなたがPHPでそれを設定することはできませんインデックステンプレートで ユーザーは実際に選択を行うので、JavaScriptを使用する必要があります。

関連する問題