How can we share data among Controllers "NOT VIEWS"

Published 5 months ago by yu0307

We probably all aware that we can share data with view composer, but that data is loaded into the blade file, I can't seem to utilize that data while inside the controller. Furthermore, I seen people tried to use a class with needed data and extend that class. but I don't really want to extend every controller to just use a global data between them.

Is there a better way? once again, Not sharing with views but controllers.

Thank you very much

Best Answer (As Selected By yu0307)
jobcerto

just put your data on the Helpers.php, then call your function. By the way, there's a auth() helper.

jlrdw
jlrdw
5 months ago (200,050 XP)

If it's not a huge amount of data, couldn't you use session? I just wouldn't use session with too much data.

yu0307

Yah, but I would imagine having session data set anywhere in my project and becoming very hard to track down where that data was actually set. like a central place like view composer where I will always know that my Data is set from that class. I guess I'm really looking for a view composer kind of deal that will work in a controller as well.

jlrdw
jlrdw
5 months ago (200,050 XP)

@yu0307 what type of data?

yu0307

Auth::user() to start out with? I know you can just pull in the facade and use it anywhere. I'm just lazy, I don't want to type that use statement on the top. LOL

jlrdw
jlrdw
5 months ago (200,050 XP)

Really, it's just a use statement, that's required. It would take longer for an alternate solution, and more typing.

yu0307

But it will save so much more time later on when you build more dynamics to user data. you gonna have to go through all controllers to make the change.

jlrdw
jlrdw
5 months ago (200,050 XP)

Then have the controllers extend a base controller.

yu0307

Man, Please read my original question. I felt like I'm explaining my question.

jlrdw
jlrdw
5 months ago (200,050 XP)

Is there a better way?

No.

jobcerto

just put your data on the Helpers.php, then call your function. By the way, there's a auth() helper.

zachleigh

Something simple like Auth::user()? Assign $user to the object in a base controller. That is by far the easiest solution.

yu0307

Build a helper function Is what I think the closest solution to my question. it's centralized and can be managed from one location without having to worried about the rest of the project.

Thanks Jobcerto.

jlrdw
jlrdw
5 months ago (200,050 XP)

Now you talk function your original question was concerning data. Data and code are two different things.

yu0307

Man, Understand what I'm asking, I asked for a way to share data. through methods, facade, helpers , session or whatever, that's for you to tell me. You got me explain my question over and over and now it seems you got your feeling hurt just because you didn't know what to say. I wish there is a way to mute a person.

jlrdw
jlrdw
5 months ago (200,050 XP)

@yu0307 was this just a "spam", question to waste people's valuable time? You yourself said

I'm just lazy

And you said

we can share data with view composer, but that data is loaded into the blade file, I can't seem to utilize that data while inside the controller.

again

data

But you meant code. So what a waste of time.

I will mute myself from any further communication with you. I promise.

Please sign in or create an account to participate in this conversation.