dtz46697 2017-05-26 08:27
浏览 549
已采纳

Laravel以admin和普通用户的身份传递值

I have a site where an admin can register themselves. Currently there is only one admin so the functions below works for passing the value:

public function getAllVideos()
{
    $videos = Video::all();
    $price = DB::table('donor')->sum('amount_donated');
    $goal = auth()->user()->goal;

    return view('adminmanagement', compact('videos', 'price', 'goal'));
}

public function changeGoal(Request $data)
{
    auth()->user()->update([
        'goal' => $data->input('newGoal')
    ]);

    return redirect('/home');
}

And if I need to just pass it to a view where a normal user can see, do I do the following?

public function getAllVideos()
    {
        $videos = Video::all();
        $price = DB::table('donor')->sum('amount_donated');

User::first()->goal;

        return view('adminmanagement', compact('videos', 'price', 'goal'));
    }

    public function changeGoal(Request $data)
    {
        auth()->user()->update([
            'goal' => $data->input('newGoal')
        ]);

        return redirect('/normalview');
    }

But what if there are more than one registered users (admin) in the system. Would it still be fine as only one admin is logged in at a time? Or does the code need to change?

EDIT: I have registration only for admins (which are 'user'), the normal users ( which are 'donors' in my case) don't have any registration/logging needed. So my main purpose is to be able to pass that $goal value to two different pages. One accessbile to admin(let's say a registered and logged in admin named jon), and other accessible to the normal user. so my current code 'User::first()->goal;' should do the trick, right? but, will it be fine if lets say, another admin named jim registers and logs in. So now the admin that is logged in is jim, not jon will it still display the $goal value in the admin's view page(accessed by jim) and normal user's view page(accessed by a normal/random person)?

  • 写回答

2条回答 默认 最新

  • dongzhang5787 2017-05-26 08:48
    关注

    From your other question you said that there's only one user and it's an admin. In that case you could replace the auth()->user() with User::first(). But if you have more than one admin or users, you need to specify the user using User::find(1) with the user id. Every user has a goal field and i assume you want to fetch only the goal from the admin user with the actual value. If so you can do this.

    Replace

    $goal = auth()->user()->goal;
    

    With

    $goal = User::find(1)->goal;
    

    Make sure to use the user id of the user with the goal value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致