D
D
Dmitry Kuznetsov2018-05-15 13:18:11
Laravel
Dmitry Kuznetsov, 2018-05-15 13:18:11

The cybercog/laravel-ban package, when unblocked, returns null and does not unblock the user, why?

Another question about the cybercog/laravel-ban package .
There is a user unlock code (after blocking):

/**
     * Разблокировка пользователя
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function unbanUser(int $id){
        $user = User::onlyBanned()->find($id);

        if($user){
            $unbanUser = $user->unban();

            if($unbanUser){
                flash('Пользователь ' . $user->login . ' успешно разблокирован!')
                    ->success();

                return redirect()->intended(route('admin.users'));
            }else{
                flash('Ошибка при разблокировке пользователя!')->warning();

                return redirect()->back();
            }
        }else{
            flash('Невероятно, но тут ошибка!)')->info();

            return redirect()->intended(route('admin.users'));
        }
    }

And the view of the unlock button:
@if($user->isBanned())
    <tr class="table-danger">
@else
    <tr>
@endif

    <th scope="row">{{ $user->id }}</th>
    <td>{{ $user->login }}</td>
    <td>{{ $user->mail }}</td>
    <td>{{ ($user->status) ? 'Активирован' : 'Не активирован' }}</td>
    <td>{{ $user->created_at }}</td>
    <td>
        @can('admin.users.banned')
            @if($user->isNotBanned())
                <a href="{{ route('admin.users.banned', $user->id) }}">
                    <i class="fa fa-ban text-danger" style="font-size: 2em;"></i>
                </a>
            @endif
        @endcan

        @can('admin.users.endBanned')
            @if($user->isBanned())
                <a href="{{ route('admin.users.endbanned', $user->id) }}">
                    <i class="fa fa-check-circle text-success" style="font-size: 2em;"></i>
                </a>
            @endif
        @endcan
    </td>
</tr>

When pressed, unlocking does not work, because. returns null in the $user->unban() method; $user itself exists and there is data.
Just in case, the User model:
<?php
namespace App;

use Cog\Contracts\Ban\Bannable as BannableContract;
use Cog\Laravel\Ban\Traits\Bannable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements BannableContract{
    use Notifiable, HasRoles, Bannable;

    protected $fillable = ['login', 'mail', 'password', 'banned_at'];
    protected $hidden = ['password', 'remember_token'];
    protected $dates = ['deleted_at', 'banned_at'];

    /**
     * Поле E-Mail адреса для уведомлений
     * @return mixed
     */
    public function routeNotificationForMail(){
        return $this->mail;
    }

    /**
     * Шифруем пароль по умолчанию функцией bcrypt.
     * @param $password
     */
    public function setPasswordAttribute($password){
        $this->attributes['password'] = bcrypt($password);
    }

    /**
     * Не даём заблокированным пользователям заходить в свой аккаунт
     * @return bool
     */
    public function shouldApplyBannedAtScope(){
        return true;
    }
}

Thanks in advance!
UPD: And if you delete deleted_at in the field (more precisely, make it null), then it gives an error: "Trying to get property 'bans' of non-object", and a new date appears in the database (Carbon::now).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iljaGolubev, 2018-05-15
@dima9595

there is no return in unban()
check $user->isBanned();
and learn how to work with documentation and source codes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question