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条)

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多