Laravel检查用户是否被禁止,如果是,请骂他的名字

I want to check if an user is banned, this works, but now I want to have a function that returns the username striked.

This is what I have now:

public static function CheckIfBanned($username)
{
    $CheckBan = Ban::orderBy('expire', 'ASC')->where('lifted', '=', '0')
                                            ->where('expire', '>', \Carbon\Carbon::now())
                                            ->where('username', '=', $username)
                                            ->get();

    if ($CheckBan)
    {
        return '<s>'.$username.'</s>';
    }
    else
    {
        return $username;
    }
}

So, this works, but it takes only the first user.

Do I need to add an foreach? If so, where and how?

Assuming your code works fine and given a list of usernames, simply calling your function in a loop will give you your desired result:

$displayableUsernames = [];
foreach($usernames as $username) {
    $displayableUsernames[] = CheckIfBanned($username);
}

With the above code snippet, you can parse through a list of plain-text usernames and add it to an array of displayable usernames that you can use.

From what I understand, however, this is a process that needs to be done EVERY time you ask for a User's username from the database.

For that reason, it might be a cleaner and more elegant solution if you used an Eloquent Accessor method that gets the User's username using your function:

class User extends Eloquent {
    public function getUsernameAttribute($value)
    {
        return CheckIfBanned($value);
    }
}

Documentation for Eloquent accessor methods can be found here: http://laravel.com/docs/4.2/eloquent#accessors-and-mutators

EDIT:

If your CheckIfBanned() function is within your Helper class, you can call it with something similar the following code in your User.php model class:

use App\Controllers\Helper;

class User extends Eloquent {
    public function getUsernameAttribute($value)
    {
        return Helpers::CheckIfBanned($value);
    }
}

Please note that I do not know what your actual namespace is for your Helper class, so you will have to import your Helper class with the proper namespace.

Using this accessor in practice, you can simply call the following code:

$user = User::find($id);

$username = $user->username; // This will automatically call your CheckIfBanned() function