首页 文章

输入::具有if语句laravel中的单选按钮值

提问于
浏览
0

所以我在我的laravel项目的页面中得到了这个表单,我的表单对URL进行了get请求 .

<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>

我明白,如果你有单选按钮,那么如果你想检查和取消选中其他按钮,那么它们的名字最好是相同但具有不同的值 .

现在我使用我的单选按钮来过滤不同类型的用户及其 balancer ,如果它为负,正或空 .

现在我想弄清楚我应该在我的控制器中的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]);
}

提前致谢

2 回答

  • 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语句,检查是否要这样做 . 's not a controller job. Instead you could delegate a piece of work that needs to be done to some other classes that are more likely to be responsible for doing this. For example, in Laravel world it'共同有 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

    如果您只想检查单选按钮状态,即检查或不检查,请尝试这样做

    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
                 }
         }
    

相关问题