在laravel中使用外键保存对象

时间:2016-06-07 13:20:23

标签: php mysql laravel foreign-keys

我使用PHP,Laravel 5.2和MySQL。

在用户注册期间,我需要创建一个新患者。但是,患者有用户ID,联系人ID和监护人ID(外键)。

当我尝试保存()患者时,我得到以下例外:

  

SQLSTATE [42S22]:未找到列:1054未知列' patient_id'在   '字段列表' (SQL:update users set patient_id = 0,updated_at =   2016-06-07 12:59:35其中id = 6)

问题是我没有 patient_id 专栏。相反,我有 patientId

我不知道如何解决这个问题。任何帮助将不胜感激。如果这很重要,我可以包含迁移文件。

UserController.php

public function postSignUp(Request $request)
{
    $this->validate($request,[
        'email' => 'required|email|unique:users',
        'name' => 'required|max:100',
        'password' => 'required|min:6'
    ]);


    $guardian = new Guardian();
    $guardian->guardianId = Uuid::generate();;
    $guardian->save();

    $contact = new Contact();
    $contact->contactId = Uuid::generate();
    $contact->save();

    $user = new User();
    $user->email = $request['email'];
    $user->name = $request['name'];
    $user->password = bcrypt($request['password']);
    $user->save();


    $patient = new Patient();
    $patient->patientId = (string)Uuid::generate();
    $patient->user()->save($user);
    $patient->contact()->save($contact);
    $patient->guardian()->save(guardian);



    $patient->save();
    Auth::login($user);

//        return redirect()->route('dashboard');
}

Patient.php

class Patient extends Model
{
    protected $primaryKey='patientId';
    public $incrementing = 'false';
    public $timestamps = true;

    public function user()
    {
        return $this->hasOne('App\User');
    }
    public function contact()
    {
        return $this->hasOne('App\Contact');
    }
    public function guardian()
    {
        return $this->hasOne('App\Guardian');
    }
    public function allergies()
    {
        return $this->belongsToMany('App\PatientToAllergyAlert');
    }
    public function medicalAlerts()
    {
        return $this->belongsToMany('App\PatientToMedicalAlert');
    }
}

user.php的

class User extends Authenticatable
{
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function patient()
    {
        return $this->belongsTo('App\Patient');
    }
}

Contact.php

class Contact extends Model
{
    protected $table = 'contacts';
    protected $primaryKey = 'contactId';
    public $timestamps = true;
    public $incrementing = 'false';

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

Guardian.php

class Guardian extends Model
{
    protected $table = 'guardians';
    protected $primaryKey = 'guardianId';
    public $timestamps = true;
    public $incrementing = 'false';

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

2 个答案:

答案 0 :(得分:5)

您尚未正确定义关系。首先,将表字段填入Patient,Contact,Guardian类中的$ fillable数组中(就像在User类中一样)。

如果您想在患者和用户之间使用hasOne关系,那么您需要患者表上的user_id字段。您也可以使用belongsTo关系。

如果要使用自定义列名称,只需在关系方法中指定它们:

public function user()
{
    return $this->hasOne('App\User', 'id', 'user_id');
    // alternatively
    return $this->belongsTo('App\User', 'user_id', 'id');
}

只需浏览文档而不跳过段落,您将在几分钟内完成:) https://laravel.com/docs/5.1/eloquent-relationships#defining-relationships

此外,这不起作用:

$patient = new Patient();
$patient->patientId = (string)Uuid::generate();
$patient->user()->save($user);

new Patient()仅创建对象,但不将其存储在DB中,因此您将无法保存关系。您需要创建对象并将其存储到DB以避免此问题:

$patient = Patient::create(['patientId' => (string)Uuid::generate()]);
$patient->user()->save($user);
...

// or
$patient = new Patient();
$patient->patientId = (string)Uuid::generate();
$patient->save();
$patient->user()->save($user);
...

答案 1 :(得分:0)

设置关系时,可以在其他模型中指定主键的名称。看here

我不确定,但我认为您的关系定义不正确。