cybercog / laravel-ban

Laravel Ban simplify blocking and banning Eloquent models.
https://komarev.com/sources/laravel-ban
MIT License
1.07k stars 63 forks source link

ban:delete-expired problem #43

Closed mohammad-y closed 5 years ago

mohammad-y commented 5 years ago

When I run ban: delete-expired, I get this error:

In BanObserver.php line 65: Call to a member function count() on null

Full error text:

[2019-06-16 05:30:01] local.ERROR: Call to a member function count() on null {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to a member function count() on null at /vendor/cybercog/laravel-ban/src/Observers/BanObserver.php:65) [stacktrace] $0 [internal function]: Cog\Laravel\Ban\Observers\BanObserver->deleted(Object(Cog\Laravel\Ban\Models\Ban)) $1 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(369): call_user_func_array(Array, Array) $2 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(200): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('eloquent.delete...', Array) $3 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(173): Illuminate\Events\Dispatcher->dispatch('eloquent.delete...', Array, false) $4 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(148): Illuminate\Events\Dispatcher->fire('eloquent.delete...', Object(Cog\Laravel\Ban\Models\Ban)) $5 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(790): Illuminate\Database\Eloquent\Model->fireModelEvent('deleted', false) $6 /vendor/cybercog/laravel-ban/src/Services/BanService.php(61): Illuminate\Database\Eloquent\Model->delete() $7 /vendor/laravel/framework/src/Illuminate/Support/Collection.php(339): Cog\Laravel\Ban\Services\BanService->Cog\Laravel\Ban\Services\{closure}(Object(Cog\Laravel\Ban\Models\Ban), 3) $8 /vendor/cybercog/laravel-ban/src/Services/BanService.php(62): Illuminate\Support\Collection->each(Object(Closure)) $9 /vendor/cybercog/laravel-ban/src/Console/Commands/DeleteExpiredBans.php(54): Cog\Laravel\Ban\Services\BanService->deleteExpiredBans() $10 [internal function]: Cog\Laravel\Ban\Console\Commands\DeleteExpiredBans->handle() $11 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array) $12 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() $13 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) $14 /vendor/laravel/framework/src/Illuminate/Container/Container.php(549): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) $15 /vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array) $16 /vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle)) $17 /vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle)) $18 /vendor/symfony/console/Application.php(960): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\BufferedOutput)) $19 /vendor/symfony/console/Application.php(255): Symfony\Component\Console\Application->doRunCommand(Object(Cog\Laravel\Ban\Console\Commands\DeleteExpiredBans), Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\BufferedOutput)) $20 /vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\BufferedOutput)) $21 /vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\BufferedOutput)) $22 /vendor/laravel/framework/src/Illuminate/Console/Application.php(177): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Symfony\Component\Console\Output\BufferedOutput)) $23 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(249): Illuminate\Console\Application->call('ban:delete-expi...', Object(Illuminate\Support\Collection), NULL) $24 /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): Illuminate\Foundation\Console\Kernel->call('ban:delete-expi...') $25 /app/Console/Kernel.php(35): Illuminate\Support\Facades\Facade::__callStatic('call', Array) $26 [internal function]: App\Console\Kernel->App\Console\{closure}() $27 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Object(Closure), Array) $28 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(75): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() $29 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Object(Closure), Object(Closure)) $30 /vendor/laravel/framework/src/Illuminate/Container/Container.php(549): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Object(Closure), Array, NULL) $31 /vendor/laravel/framework/src/Illuminate/Console/Scheduling/CallbackEvent.php(74): Illuminate\Container\Container->call(Object(Closure), Array) $32 /vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php(59): Illuminate\Console\Scheduling\CallbackEvent->run(Object(Illuminate\Foundation\Application)) $33 [internal function]: Illuminate\Console\Scheduling\ScheduleRunCommand->handle() $34 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array) $35 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() $36 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) $37 /vendor/laravel/framework/src/Illuminate/Container/Container.php(549): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) $38 /vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array) $39 /vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) $40 /vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) $41 /vendor/symfony/console/Application.php(960): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) $42 /vendor/symfony/console/Application.php(255): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Console\Scheduling\ScheduleRunCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) $43 /vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) $44 /vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) $45 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(121): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) $46 /artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) $47 {main} "}

antonkomarev commented 5 years ago

It looks like model which you are trying to ban is not using Bannable trait: https://github.com/cybercog/laravel-ban#prepare-bannable-model

antonkomarev commented 5 years ago

I'm closing this issue since there is no response and it might be already solved. Feel free to continue conversation here.

dreigningking commented 5 years ago

Is it necessary to delete ban? Why not just check if the expired_at time is > than now( ). If yes, allow user If no, don't allow user? I'm thinking of editing your code to behave like this in my project so that I don't need to set cron job. What do you advise ?

antonkomarev commented 5 years ago

@reigningkingforever If you don't want to use CRON job - then you could create a middleware and check if user ban is expired on login and delete this ban before the login.