簡體   English   中英

使用 TextField laravel Nova 將數據添加到資源中

[英]Adding Data to Belongs to resource using TextField laravel Nova

嗨,有什么方法可以在一個請求中在兩個表中插入數據 laravel nova 所屬關系 我想要實現的是我有兩個模型,第一個是用戶模型,第二個是學生模型用戶模型有電子郵件和密碼並且當創建學生時,學生模型具有學生的其他分析詳細信息我還需要在用戶表中插入一條記錄,並將插入的實體與我的學生詳細信息相關聯,例如某些實體進入學生詳細信息,而某些實體進入與身份驗證相關的用戶模型我正在嘗試使用 laravel nova 實現這一目標。

這是與學生的用戶模型關系

 public function students(){
    return $this->hasMany('App\student','user_id','id');
}

這是學生模型與用戶的關系

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

我也嘗試使用 nova 屬於字段,但它為我提供了現有用戶的下拉列表,但我想一次性創建用戶和學生並將它們相互鏈接https://nova.laravel.com/docs/ 2.0/resources/relationships.html#belongsto

我希望我能澄清我的觀點,如果有任何問題可以隨時提出或合作,謝謝

您可以在模型中執行此操作,您需要覆蓋保存功能:

public function save(array $options = array()) {
    parent::save($options);
}

請注意,為了將您當前的模型鏈接到另一個模型,您需要在此行之后這樣做 parent::save($options); 因為在將模型實際保存在數據庫中之前,您無法獲得當前記錄的 $this->"primary_key"

前任:

class Student
{
    public function save(array $options = array()) {

        $user = new User();
        $user->property1 = '';
        $user->property2 = '';
        $user->save();

        parent::save($options);

        //Here you can get the $this->studentid
        //Or the $user->userid

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM