【问题标题】:Input::has for radio button value in if statement laravelInput::has for if 语句 laravel 中的单选按钮值
【发布时间】:2015-09-17 14:51:36
【问题描述】:

所以我在我的 laravel 项目的页面中得到了这个表单,我的表单对 url 进行了获取请求。

<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]);
}

提前致谢

【问题讨论】:

    标签: php laravel input


    【解决方案1】:

    就像上面建议的那样,您可以在控制器中进行简单的检查。要成为更像 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]);
    }
    

    当然,您可以使用第一种方法,它可能更简单、更易于使用且更清晰。但是,一旦您开始维护包含大量内容的大型应用程序,您很快就会意识到后一种方法的重要性。

    【讨论】:

    • 非常感谢,这真的很有帮助。也感谢存储库提示,我一定会尝试的!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-07-21
      • 1970-01-01
      • 2012-11-21
      • 2016-08-04
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 2013-08-03
      相关资源
      最近更新 更多