0

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?

2
  • Are you trying to print a single username or multiple?
    – eluong
    Oct 8, 2015 at 17:28
  • I want to display this on multiple usernames. Something like they have in MyBB. So when a user is banned, he needs to be displayed with a strike <s>username</s>, else he needs to be displayed the normal way.
    – Robin
    Oct 8, 2015 at 17:30

1 Answer 1

0

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
9
  • So I need to add the first snippet to my function CheckIfBanned() and the second to the userClass? Or how should it be done? :p
    – Robin
    Oct 8, 2015 at 18:06
  • I have the first function standing an my class Helper that extends BaseController I don't know if this can give any issues?
    – Robin
    Oct 8, 2015 at 18:13
  • The second snippet is my personal preference as a solution to your problem. Yes, add the above getUsernameAttribute() method to your User class. I gave two solutions here. The first snippet is for when you want to construct an array/list of displayable usernames to use. You are not adding the first snippet to your function CheckIfBanned(), you are utilizing the function within a loop, rather. Sorry if it wasn't clear.
    – eluong
    Oct 8, 2015 at 18:16
  • Okay, Class 'Controllers\Helper' not found is the error I get now, But the class Helper does exist?! Class helper does extend the BaseController, can this cause the issue?
    – Robin
    Oct 8, 2015 at 18:21
  • What is the namespace of your Helper class? It should be at the top of your file
    – eluong
    Oct 8, 2015 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.