class User extends Authenticatable{ public function profile() { return $this->hasOne(Profile::class); }}
class Profile extends Model public function user() { return $this->belongsTo(User::class); } }
上面是一張user表和profile表的Model,兩張表一對一關聯性,當使用者登入後,可以建立他的profile,然後提交。
可是,第二次訪問這個建立profile的表單頁,也能提交成功,這哪裡是一對一,這不是一對多麼!是哪裡有錯,還是模型中進行一對一關聯後,要保證profile只有一條記錄,在控制器中還要進行判斷?
回複內容:
class User extends Authenticatable{ public function profile() { return $this->hasOne(Profile::class); }}
class Profile extends Model public function user() { return $this->belongsTo(User::class); } }
上面是一張user表和profile表的Model,兩張表一對一關聯性,當使用者登入後,可以建立他的profile,然後提交。
可是,第二次訪問這個建立profile的表單頁,也能提交成功,這哪裡是一對一,這不是一對多麼!是哪裡有錯,還是模型中進行一對一關聯後,要保證profile只有一條記錄,在控制器中還要進行判斷?
要是能建立多個,那就不是一對一了唄
資料庫是人設計的,又不是死的,需求是什麼樣的就設計成什麼樣的,如果是一對一,那就不能提交兩個,如果能提交多個,那就是一對多
提交後是重新覆蓋那一條資料就可以一對一咯。
首先查庫看是否在profile已經有 該使用者的記錄了,如果有則update 更新 否則 insert插入記錄
這個是你業務設計的問題,你需要在業務中去控制,如果出現了不符合一對一的情況,可以認為出現了資料不一致。