如何将不明确的用户名存储到数据库字段中

时间:2019-02-16 17:19:05

标签: laravel

我的治疗表中有一个临床医生领域。我希望将经过身份验证的用户存储在表的临床医生字段中。 数据库迁移中的字段为

$table->string('clinician')->nullable();

在我的TreatmentController中,我有thisFacade

use Illuminate\Support\Facades\Auth; 

此功能

    public function store (Patient $patient)
        {
        Treatment::create([
            'patient_id'=>$patient->id,
            'clinician'=> Auth::user()->username,
            'treatmentDetails' => request('treatmentDetails') 
        ]);
        return back()->with('success','Treatment saved!.');
        }
I

在治疗模型中,我有这个。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Treatment extends Model
{
    //
    protected $guarded = [];

    public function patients()
    {
        return $this->belongsTo('App\Patient');
    }
}

var_dump(Auth :: user());返回此

object(App\User)#615 (27) { ["fillable":protected]=> array(3) { [0]=> string(8) 
"username" [1]=> string(5) "email" [2]=> string(8) "password" } 
["hidden":protected]=> array(2) { [0]=> string(8) "password" [1]=> string(14) 
"remember_token" } ["connection":protected]=> string(5) "mysql" 
["table":protected]=> string(5) "users" ["primaryKey":protected]=> string(2) 
"id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) 
["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } 
["perPage":protected]=> int(15) ["exists"]=> bool(true) 
["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(9) { 
["id"]=> int(1) ["email"]=> string(16) "hans10@gmail.com" 
["email_verified_at"]=> NULL ["password"]=> string(60) 
"$2y$10$3KfEDTP1Vf014KZyvvH4Nu1thiLQtfKxz6Jo7wFpqAoKMg6SSRb2G" ["username"]=> 
string(5) "Karim" ["roleId"]=> int(2) ["remember_token"]=> NULL 
["created_at"]=> string(19) "2019-02-16 13:00:59" ["updated_at"]=> string(19) 
"2019-02-16 13:00:59" } ["original":protected]=> array(9) { ["id"]=> int(1) 
["email"]=> string(16) "hans10@gmail.com" ["email_verified_at"]=> NULL 
["password"]=> string(60) 
"$2y$10$3KfEDTP1Vf014KZyvvH4Nu1thiLQtfKxz6Jo7wFpqAoKMg6SSRb2G" ["username"]=> 
string(5) "Karim" ["roleId"]=> int(2) ["remember_token"]=> NULL 
["created_at"]=> string(19) "2019-02-16 13:00:59" ["updated_at"]=> string(19) 
"2019-02-16 13:00:59" } ["changes":protected]=> array(0) { } 
["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } 
["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } 
["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> 
array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> 
array(0) { } ["timestamps"]=> bool(true) ["visible":protected]=> array(0) { } 
["guarded":protected]=> array(1) { [0]=> string(1) "*" } 
["rememberTokenName":protected]=> string(14) "remember_token" }

如果Karim是当前登录的用户,我希望在临床医生字段中插入Karim名称。治疗已成功存储,但在临床医生字段中值为空。

1 个答案:

答案 0 :(得分:0)

尝试在模型中使用$fillable属性。

https://laravel.com/docs/5.7/eloquent