Support the ongoing development of Laravel.io →
Database Eloquent Views
Last updated 2 years ago.
0

I would recommend adding a is_banned boolean column to your users table.

This way, when you ban a user, you would do two things:

  1. Change the user's is_banned attribute to true to indicate that he is banned.
  2. Add the user to the banned_users table with a timestamp indicating until when. If you want a permanent ban, you can just set the timestamp to NULL since that sort of indicates banning them for an indefinite period of time.

Once you do that, you can get all non-banned users by doing this:

// Add to Users model
protected $casts = ['is_banned' => 'boolean'];
// Eloquent query to get all non-banned users.
$users = Users::orderBy('id', 'desc')->where('is_banned', '=', false)->paginate(6);
Last updated 9 years ago.
0

I'm questioning why you would want another table? I can't think of a good reason. And you have to deal with keeping the integrity between two tables. If you truly want another table then I would make a Banned model and relate it with a hasOne.

// user model
public function banned()
{
return hasOne('Banned');
}

// then query
$users = Users::has('banned', '=', 0)->get();

But again I question why you would have a separate table.

0

@rickshawhobo It depends on what he wants to do. A banned user doesn't just have to be a binary thing (banned and unbanned).

You might want to know who banned the user, when he was banned, how long he will be banned, etc. You might want to track his history or keep a list of reasons as to why he was banned.

0

@thomastkim @rickshawhobo I already have a is_banned column, but as you said, I need to have some informations about the banned users, like timestamps, reasons, ip...

Thank you for your suggestions! I used it and it's working!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

WilliamB williamb Joined 20 Jun 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.