【问题标题】:How to get current user details in Laravel 5.7?如何在 Laravel 5.7 中获取当前用户详细信息?
【发布时间】:2020-05-06 00:50:22
【问题描述】:

抱歉,我的声誉低于 10 岁,而且我是新来这里的,所以我无法发布图片。我对此有疑问,我有 2 列“created_by”和“modified_by”,我想通过用户 id 在后端进入数据库,但它向我显示了错误,我不知道该怎么做。谁能帮帮我

    $policy = new Policy([
        'id' => $request->get('id'),
        'policy_category_id' => $request->get('policy_category_id'),
        'policy_title' => $request->get('policy_title'),
        'version_no' => $request->get('version_no'),
        'policy_details' => $request->get('policy_details'),
        'expiry_date' => $request->get('expiry_date'),
        'file_path' => $request->get('file_path'),
        'created_by'=> auth()->user(),
        'modified_by'=>auth()->user()
    ]);



    try {
        $policy->save();
    } catch (\Illuminate\Database\QueryException $e) {
        $errorCode = $e->errorInfo[1];
        if ($errorCode == '1062') {
            $status1 = 'failed';
            $statusMsg1 = 'Failed to Create, Duplication Code ' . $request->get('code') . '!';
            return redirect()->back()->with($status1, $statusMsg1);
        }

    }

错误 ( Illuminate\Database\QueryException SQLSTATE [42S22]:找不到列:1054 '字段列表'中的未知列'created_by'(SQL:插入company_policyidpolicy_category_idpolicy_titleversion_nopolicy_details,@ 987654329@, file_path, created_by, updated_at, created_at) 值 (333, 333, 333, 333, 333, 3333-03-31, 33, {"id":1,"name": "约翰史密斯","email":"john_smith@gmail.com","email_verified_at":null,"created_at":"2019-10-21 01:54:14","updated_at":"2019-10-21 01:54:14"}, 2020-01-20 01:26:41, 2020-01-20 01:26:41)) http://localhost:8089/policy)

【问题讨论】:

  • 您确定您的策略表中有 created_by 字段吗?
  • 是的,我确定@Qonvex620

标签: php mysql laravel


【解决方案1】:

以下是您获取经过身份验证的用户详细信息的方式

获取当前用户的所有详细信息

Auth::user() // returns an instance of the authenticated user...

获取当前用户的id

Auth::id()  // returns the id of the authenticated user...

获取当前用户的属性

Auth::user()->firstname
Auth::user()->lastname

【讨论】:

  • 我的登录用户是邮箱和密码
  • 这只是一个例子,但您只能选择您想要获取的详细信息。
【解决方案2】:

你有两个选择:

   1) use Illuminate\Support\Facades\Auth;

      Auth::user()->id;

   2) auth()->user()->id;

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2012-03-02
    • 1970-01-01
    • 2017-10-06
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多