简体   繁体   中英

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.

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

Now im trying to figure out what i should write in my if statement in my controller. Before i tried checkboxes and used if(Input::has('name')) and so on. 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?

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

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

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.

Create a class in the 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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