Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I add middleware to log automatically #13

Closed
Expertweblancer opened this issue Jun 2, 2020 · 16 comments
Closed

How can I add middleware to log automatically #13

Expertweblancer opened this issue Jun 2, 2020 · 16 comments
Labels
good first issue Good for newcomers

Comments

@Expertweblancer
Copy link

Detailed description

Hello
I am using Laravel 7.0
composer install and migration successfully done
I wanted to add middleware to log automatically.
I tried many times but failed
Can you give me a tip to solve this issue

Context

I want to track the visitors on my web admin panel

Possible implementation

middleware issue

Your environment

Include as many relevant details about the environment you experienced the bug in and how to reproduce it.

  • Version used (e.g. PHP 7.2, HHVM 3):
  • Operating system and version (e.g. Ubuntu 19.04, Windows 10):
  • Link to your project:
  • ...
  • ...
@khanzadimahdi
Copy link
Member

add the below middleware into your route middlewares:

Shetabit\Visitor\Middlewares\LogVisits

then use Shetabit\Visitor\Traits\Visitable trait in all models which can be visited by users.

and use Shetabit\Visitor\Traits\Visitor trait in your user model.

@Expertweblancer
Copy link
Author

http://prntscr.com/ssdkhh
http://prntscr.com/ssdkzz

And then what is the next step?
I cant still see the logs in the database visitor tables

@khanzadimahdi
Copy link
Member

khanzadimahdi commented Jun 2, 2020

you have introduced the visitors middleware to Laravel's kernel.

add it on the routes that you want their logs:
for example in routes/web.php :

Route::middleware(['visitors'])->get('/', 'exampleController@method');

you have imported trait in your model, then you should use it in model like the below:

class YourModel extends Model {
    use Visitable;

}

@khanzadimahdi khanzadimahdi added the good first issue Good for newcomers label Jun 2, 2020
@Expertweblancer
Copy link
Author

I already tried like that before
I am getting errors
This is the log file
thanks for your help

https://www.dropbox.com/s/9g9fva3yxxunxf8/laravel.log?dl=0
hope you to help me finally

@Expertweblancer
Copy link
Author

[2020-06-02 14:07:28] local.ERROR: Target class [App\Http\Shetabit\Visitor\Middlewares\LogVisits] does not exist. {"exception":"[object] (Illuminate\Contracts\Container\BindingResolutionException(code: 0): Target class [App\Http\Shetabit\Visitor\Middlewares\LogVisits] does not exist. at E:\2020\5.

@khanzadimahdi
Copy link
Member

in your kernel, add a back slash before middleware's path:

 'visitors' => \Shetabit\Visitor\Middlewares\LogVisits::class,

the error is because of that.

@Expertweblancer
Copy link
Author

same issue
I am finding the issue

@Expertweblancer
Copy link
Author

[2020-06-02 14:17:07] local.ERROR: Driver not selected or default driver does not exist. {"exception":"[object] (Shetabit\Visitor\Exceptions\DriverNotFoundException(code: 0): Driver not selected or default driver does not exist. at E:\2020\5. May\laravel-movies\vendor\shetabit\visitor\src\Visitor.php:341)
[stacktrace]

@Expertweblancer
Copy link
Author

Driver not selected or default driver does not exist.

@khanzadimahdi
Copy link
Member

publish package configs:

php artisan vendor:publish

a config file must be exists after that: config/visitor.php

@Expertweblancer
Copy link
Author

I already publish vendor
There is visitor.php in config directory

@khanzadimahdi
Copy link
Member

have you cached configs? you need to clear cache

php artisan config:clear

@Expertweblancer
Copy link
Author

Wow
php artisan config:cache

@Expertweblancer
Copy link
Author

You are great.
What is your email address or preferred communication?
If I have any project, we can work together
Thanks for your valuable time

@khanzadimahdi
Copy link
Member

Your welcome.
my email: [email protected]

@khanzadimahdi khanzadimahdi pinned this issue Jun 2, 2020
@dunialmasmari
Copy link

it will be a good idea if you added this explanation to your install and config details(the setup documentation) ,good job, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants