Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstnorris's avatar

I don't personally give all details of what I doing but i will this time and I hope i can get a good feedback, no things like ... why your title is like that or whatever! I won't explain all thing in the title!

I'm not sure what you mean by this, but if you are saying you "don't like" to give information then I would say "you're wrong" as you need to give us information in order for us to help you.

socieboy's avatar

I say that because once I did before and the only thing i got was bad reviews.

gwp's avatar

Now we've got more information, why don't we try to get to a solution rather than going down this road?

1 like
mstnorris's avatar

@socieboy don't put it all in the title, but put as much information, as concisely and clearly as possible in the main thread. That is all.

Anyway, let's get to the bottom of this issue. Has anything we've suggest thus far helped you get any closer to what you want?

JohnRivs's avatar

So you have or are building a package (the chat package)? What about requiring the Laravel's Auth component and write a function on that package responsible for checking if there's a user authenticated?

1 like
mstnorris's avatar

@socieboy you didn't answer @JohnRivs. Nor did you answer me...

Has anything we've suggest thus far helped you get any closer to what you want?

socieboy's avatar

I already posted the information guys! so! ?

mstnorris's avatar

@socieboy we're not at your beck and call. If you're not willing to help us, why on earth would we help you.

socieboy's avatar

This is a Chat package for Laravel applications, I try to do it as easy is possible for other users.

I have my folder with my package with my own routes.php file.

I have added to the session table the field user_id, wish is nullable.

So now i need to update the user_id every time the users moved on the app, the Chat package could be install over different apps or websites, so I can't place the logic to get the auth user on the Controller class, I need other way?

gwp's avatar

Are you not even going to look at @JohnRivs suggestion?

Snark and copy-pasting posts aren't going to motivate us to help you, by the way.

What have you tried? What have you searched for? What are possible solutions that you don't like?

mstnorris's avatar

Did this not help you?

Create a BaseController that your normal UsersController, ArticlesController, PagesController or whatever extend so that the functionality that you're after is in each "request".

Controller Controller

class BaseController extends Controller {
    public function __construct() {
        if ( auth()->user() ) {
            // save auth()->user()->id to database
        } else { 
            // save null to database (bear in mind you will have a LOT of nulls
        } 
    }
}

BaseController Controller

class UsersController extends BaseController {
    // standard stuff here
}

We can't write the app for you. What have you tried?

socieboy's avatar

Yes I tried and is not working! I will figure out! don't waste your time anymore! Thank you!

jekinney's avatar

auth() is available almost everywhere. Guess not in the routes.php, but not a good idea I think anyways.

The point of auth() is to give that user object to be always available. If you need to fire and event for each request to update a database use an event and a global listener. In the event listener perform your task.

1 like
jekinney's avatar

By the way, for the user id use auth()->id().

1 like
JohnRivs's avatar

@gwp @mstnorris I think @socieboy doesn't want to use the controller solution becase of what he said earlier:

So now i need to update the user_id every time the users moved on the app, the Chat package could be install over different apps or websites, so I can't place the logic to get the auth user on the Controller class, I need other way?

So, as I mentioned in a previous post (that seems to be missed by @socieboy), if you don't want to put the logic in the controllers, the other place to put it is in the package itself (the one you say it's going to be installed in different apps). What you do is, you require Laravel's auth component in the package's composer.json. Then you offer some sort of function that consumes the Auth component, to determine if a user is logged in (or whatever you need).

Now here's the thing: that approach assumes Laravel, is 100% coupled to it. If that's gonna be the case, why don't you just create the package and mention in the readme.md that the user installing the package needs to do ______ in the base controller. Or even better: provide a trait with said logic, and make the developer import and use the trait in the base controller. Or make them register your service provider, where you apply the logic.

Plenty of packages do that; they provide some functionality but they ask you to write something in your own code or import something the package provides.

Please, before saying 'it would not work', just try it. Toy around with it.

2 likes
socieboy's avatar

I understand that you said! I Could do that, but I try to make a package easy to install and set up. If there is no way to do what I need without tell to developers add this specific line of code to your app to set up the package.... I'll do it!

Previous

Please or to participate in this conversation.