Laravel: (Session?) Pass value from admin side to user

孤人 提交于 2020-01-07 08:10:54

问题


I have a value called 'goal' in admin.blade page that can be changed by the admin who is logged in. I want to be able to pass that new value on a user's page(user.blade). (There is no registered user, just a normal user accessing the page). Currently, I am able to reflect the change on the admin page, but I cannot pass the value to the user page (probably because of auth).I have tried different ways but couldn't get what i wanted. Below is the function that updates the 'goal' value when i change it:

public function changeGoal(Request $data){

auth()->user()->update([
            'goal' => $data->input('newGoal')

        ]);

        return redirect('/adminpage');
}

Using this function, when i type {{$goal}}, it shows the changed value in the admin page(for a logged in admin.).So, right now, the admin can change the value and see the new value.

I want the value changed by admin to show up in the user's page (user.blade). How can I achieve that?

I was suggested using session, but it didn't work, or maybe I did something wrong (even though I tried to do it from the laravel documentation, couldn't really figure it out properly). Help would be appreciated.Thanks


回答1:


Don't attach goal to your User model. Maybe you can create a SiteData model, which has a goal attribute. Then in your user.blade.php file you can retrieve goal by:

{{ SiteData::first()->goal }}

And for admin to update it:

SiteData::first()->update(['goal' => 1000]);

Hope that helps!



来源:https://stackoverflow.com/questions/44251865/laravel-session-pass-value-from-admin-side-to-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!