2015-06-30 103 views
0

因此,我在我的laravel項目的頁面中獲得了此表單,其中我的表單獲取了對URL的請求。輸入::有if語句中的單選按鈕值laravel

<div class="col-sm-2 form-group"> 
    <div class="radio"> 
     <label><input name="BalanceType" type="radio" value="pos"> 
     Positiv</label> 
    </div> 
</div> 

<div class="col-sm-2 form-group"> 
    <div class="radio"> 
     <label><input name="BalanceType" type="radio" value="neg"> 
     Negativ</label> 
    </div> 
</div> 

<div class="col-sm-2 form-group"> 
    <div class="radio"> 
     <label><input name="BalanceType" type="radio" value="null"> 
     Noll</label> 
    </div> 
</div> 

伊夫瞭解,如果你要去有單選按鈕,然後他們的名字好是相同的,但使用不同的值,如果你要檢查,並取消其他人。

現在我使用我的單選按鈕是過濾不同類型的用戶和他們的平衡,如果它的消極,積極或空。

現在我試圖找出我應該寫在我的控制器中的if語句。在我嘗試複選框並使用if(Input :: has('name'))等等。這工作,但這只是爲不同名稱的複選框,現在我想使用單選按鈕。

我的問題是,我如何檢查是否有一個具有特定類型值的單選按鈕被選中,就像我用複選框和Input :: has做的一樣?

我的控制器:

public function balanceCheck() 
{ 
    $user = Auth::user(); 
    $users = Auth::user()->school->users()->orderBy('id', 'DESC')->get(); 


    if() 
    { 
     $users = Auth::user()->school->users()->where('balance', '>', 0)->orderBy('id', 'DESC')->get(); 
    } 
    if() 
    { 
     $users = Auth::user()->school->users()->where('balance', '<', 0)->orderBy('id', 'DESC')->get(); 
    } 
    if() 
    { 
     $users = Auth::user()->school->users()->where('balance', '=', 0)->orderBy('id', 'DESC')->get(); 
    } 

    return View::make('admin.overview', ['user' => $user, 'users' => $users]); 
} 

在此先感謝

回答

0

像上文建議,你可以在你的控制器中做一個簡單的檢查。更Laravel般的風格,你可以做

$balanceType = Input::has('BalanceType') ? Input::get('BalanceType') : null; 

if ($balanceType == 'pos') { 
    $users = Auth::user()->school->users()->where('balance', '>', 0)->orderBy('id', 'DESC')->get(); 
} elseif ($balanceType == 'neg') { 
    $users = Auth::user()->school->users()->where('balance', '<', 0)->orderBy('id', 'DESC')->get(); 
} elseif ($balanceType == 'null') { 
    $users = Auth::user()->school->users()->where('balance', '=', 0)->orderBy('id', 'DESC')->get(); 
} 

這應該完成你所需要的,但是這不是推薦的方法去用的。看,你的控制器很快就會開始看起來像一團糟。用所有這些if語句,檢查是否要做那個或那個。這不是一個控制器工作。相反,您可以將需要完成的工作委託給其他更有可能負責完成此工作的類。例如,在Laravel世界中,通常有Repositories

將存儲庫視爲與您交互的類模型。它可以執行不同的數據庫查詢並獲取您需要的任何內容,只需一行代碼就可以使控制器變得非常乾淨,從而在您的存儲庫類中調用適當的方法。不用說,如果你需要在應用程序的不同部分完全相同的查詢,那麼以後可以重新使用存儲庫中的代碼。例如,對於您的場景,您可能有一個UserRepository類。

app/Repositories/UserRepository.php

<?php 

namespace Repositories; 

class ArticleRepository 
{ 

    /** 
    * @var User 
    */ 
    protected $model; 

    /** 
    * @param User $model 
    */ 
    public function __construct(User $model) 
    { 
     $this->model = $model; 
    } 

    public function getByBalanceType($balanceType) 
    { 
     if (! $balanceType) return null; 
if ($balanceType == 'pos') { 
    $users = $this->model->school->users()->where('balance', '>', 0)->orderBy('id', 'DESC')->get(); 
} elseif ($balanceType == 'neg') { 
    $users = $this->model->school->users()->where('balance', '<', 0)->orderBy('id', 'DESC')->get(); 
} elseif ($balanceType == 'null') { 
    $users = $this->model->where('balance', '=', 0)->orderBy('id', 'DESC')->get(); 
} 
     return $users; 
    } 
} 

創建課程後,在你的控制器,你可以在構造函數中注入該存儲庫。

public function __construct(UserRepository $userRepository) 
{ 
    $this->userRepository = $userRepository; 
} 

,然後,你balanceCheck方法看起來

public function balanceCheck() 
{ 
    $user = Auth::user(); 
    $users = Auth::user()->school->users()->orderBy('id', 'DESC')->get(); 
    $balanceType = Input::has('BalanceType') ? Input::get('BalanceType') : null; 
    $users = $this->userRepository->getByBalanceType($balanceType); 
    return View::make('admin.overview', ['user' => $user, 'users' => $users]); 
} 

當然你可以用第一種方法去,這是更簡單,使用更方便,更清晰,大概。但是,一旦你開始維護大量的應用程序,並且很快就會發現後一種方法的重要性。

+0

非常感謝你,這真的很有幫助。也感謝知識庫提示,我一定會嘗試一下! –

0

如果你想只檢查單選按鈕的狀態,即它檢查或不低於嘗試這樣

if(isset($_POST['BalanceType'])){ 
     $radio_input=$_POST['BalanceType']; 
     if($radio_input =="some value"){ 
     //do some thing..... 
     }elseif($radio_input =="some other value"){ 
     //do some thing else..... 
     }else{ 
       //the last condition goes here 
      } 
    }