2017-06-05 80 views
2

我爲用戶輕鬆開啓或關閉帖子製作自定義視圖。像這張圖片一樣。無法在Laravel中切換活動/非活動狀態行

screenshot_2017-06-05_16-02-19

我有這樣的錯誤:

POST http://127.0.0.1:8000/admin/apartments/1/active 404(未找到)。

Controller/Admin/ApartmentController.php

public function index(Request $request) 
{ 
    $variations = Apartment::all(); 
    $dataType = DataType::where('slug', '=', 'products')->first(); 
    return view('voyager::apartments.browse', compact('dataType', 'variations')); 
} 
public function active(Apartment $apartment) 
{ 
    $apartment->active = true; 
    $apartment->save(); 
} 

resources/views/vendor/voyager/apartments/browse.blade.php

<tbody> 
    @foreach($variations as $data) 
     <tr> 
      <?php $checked = ($data->active == 1) ? true : false; ?> 
      <td> 
       <input type="checkbox" name="active" data-id="{{$data->id}}" class="toggleswitch toggleactive" @if($checked) checked @endif> 
      </td> 
     </tr> 
    @endforeach 
</tbody> 

@section('javascript') 
    <!-- DataTables --> 
    <script> 

     $(document).ready(function(){ 
      $('.toggleswitch').bootstrapToggle(); 
     }); 

     function parseActionUrl(action, id) { 
      if (action.match(/\/[0-9]+$/)) { 
       return action.replace(/([0-9]+$)/, id); 
      } 
      return action + '/' + id; 
     } 

     $('.toggleactive').on('change', function() { 
      $.post('/admin/apartments/' + $(this).data('id') + '/active', { active: ($(this).is(':checked')) ? 1 : 0, _token : '{{ csrf_token() }}' }, function(){ 
       toastr.success("Updated success: apartment is actived"); 
      }); 
     }); 


    </script> 
@stop 

...和routes/web.php

Route::group(['prefix' => 'admin'], function() { 
    Voyager::routes(); 

    Route::post('apartments/:id/active', '[email protected]'); 

    Route::group(['middleware' => ['admin.user'], 'as' => 'voyager.'], function() { 
     Route::resource('apartments', 'Admin\ApartmentController'); 
    }); 
}); 

注:

如果我想單擊按鈕On時顯示吐司消息,如:The apartment is enabled。否則當點擊按鈕Off時,它將顯示按鈕The apartment is disabled

有最好的方法來改變這段代碼?

$('.toggleactive').on('change', function() { 
    $.post('/admin/apartments/' + $(this).data('id') + '/active', { active: ($(this).is(':checked')) ? 1 : 0, _token : '{{ csrf_token() }}' }, function(){ 
     toastr.success("Updated success: apartment is actived"); 
    }); 
}); 
+1

運行'php artisan route:list'並檢查路由是否已註冊並在此處發佈結果。如果不是,那麼運行'php artisan route:clear'。 – Sandeesh

回答

1

將您的路線更改爲此。

Route::post('apartments/{apartment}/active', 'Admin\[email protected]'); 

還要改變你的控制器方法。

public function active(Apartment $apartment) 
{ 
    $apartment->active = request('active'); 
    $apartment->save(); 
} 

編輯:做這個基礎上,要求更新您的敬酒的消息。

public function active(Apartment $apartment) 
{ 
    $status = request('active'); 
    $apartment->active = $status; 
    $apartment->save(); 

    return $status; 
} 

$('.toggleactive').on('change', function() { 
    $.post('/admin/apartments/' + $(this).data('id') + '/active', { active: ($(this).is(':checked')) ? 1 : 0, _token : '{{ csrf_token() }}' }, function(data){ 
     toastr.success("Updated success: The apartment is " + (data == 1 ? 'enabled' : 'disabled')); 
    }); 
}); 
+0

你能解釋一下:'{apartment}'。原始我有'Admin \ ApartmentController @ active'。我的打字錯誤。 – vanloc

+0

您正在使用路由模型綁定。因此,您需要使用與Laravel的控制器方法中的參數相同的url段來解析模型。沒有這些,你不會在你的方法中收到「Apartment」模型。 – Sandeesh

+0

是的,你的回答是正確的。它會轉到:'toastr.success'。但我不知道爲什麼它不會改變我的數據庫中'active'字段的值。看來我的代碼中有一個錯誤。你能找到嗎?謝謝。 – vanloc