简体   繁体   English

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

[英]Input::has for radio button value in if statement laravel

So i got this form in a page for my laravel project where my form does a get request to the url. 因此,我在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>

Ive understood that if you gonna have radio buttons then their name better be the same but with different values if you want to check and uncheck the others. 我已经理解,如果您要具有单选按钮,则它们的名称最好是相同的,但是如果要选中和取消选中其他按钮,则它们的值应该不同。

Now what i use my radio buttons for is filtering different type of users and their balance if its negative, positive or null. 现在,我使用单选按钮的目的是过滤不同类型的用户及其余额(如果为负,为正或为null)。

Now im trying to figure out what i should write in my if statement in my controller. 现在我试图弄清楚我应该在控制器的if语句中写些什么。 Before i tried checkboxes and used if(Input::has('name')) and so on. 在我尝试复选框并使用if(Input :: has('name'))之前,依此类推。 This worked but that was only for checkboxes with different names, now i want to use radio buttons. 这有效,但这仅适用于具有不同名称的复选框,现在我想使用单选按钮。

My question is, how do i check if a radio button with a specific type of value is checked like i did with checkboxes and Input::has? 我的问题是,如何检查具有特定类型值的单选按钮,是否像复选框和Input :: has一样进行检查?

My controller: 我的控制器:

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

Thanks in advance 提前致谢

if You want to only check the radio button status ie it check or not than try like this 如果您只想检查单选按钮的状态,即检查或不检查,请像这样尝试

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

Like suggested above, you can do a simple check in you controller. 像上面建议的一样,您可以在控制器中进行简单的检查。 To be more Laravel-like style you can do 要更像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();
}

this should accomplish what you need but this is not a recommended approach to go with at all. 这应该可以满足您的需求,但这绝对不是推荐的方法。 See, very quickly your controller will start to look like a mess. 瞧,您的控制器很快就会看起来像一团糟。 With all those if statements, checks if to do that or that. 使用所有这些if语句,检查是否要执行该操作。 It'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's common to have Repositories . 例如,在Laravel世界中,通常有Repositories

Think of repository as a class that interacts with you models. 将存储库视为与您的模型进行交互的类。 It may does different DB queries and fetch whatever you need, leaving the controller nice and clean with only one line of code to invoke the appropriate method on your repository class. 它可以执行不同的数据库查询并获取您所需的任何内容,仅用一行代码即可调用存储库类中的适当方法,从而使控制器保持整洁美观。 It's not to mention, that later you could reuse the code from your repository, if you need exact same query in a different part of your app. 更不用说,以后,如果您需要在应用的不同部分中进行完全相同的查询,则可以重用存储库中的代码。 For example, for your scenario, you might have a UserRepository class. 例如,对于您的方案,您可能具有UserRepository类。

Create a class in the app/Repositories/UserRepository.php 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;
    }
}

After that in your controller you could inject this repository in constructor. 之后,您可以在控制器中将此存储库注入构造函数中。

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

ANd then, your balanceCheck method would look 然后,您的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]);
}

but of course you can go with first approach, it's much simpler, easier to use and more clear, probably. 但是当然可以采用第一种方法,它可能更简单,更易于使用且更清晰。 But once you get to maintain large apps with lots of stuff going on, you'll quickly mind the significance of the latter approach. 但是,一旦您维护具有大量功能的大型应用程序,便会很快意识到后一种方法的重要性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM