第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Laravel 如何向 2 個(gè)表中插入數(shù)據(jù)?

Laravel 如何向 2 個(gè)表中插入數(shù)據(jù)?

PHP
青春有我 2022-12-30 17:07:47
我的用戶模型<?phpnamespace App;use Illuminate\Contracts\Auth\MustVerifyEmail;use Illuminate\Foundation\Auth\User as Authenticatable;use Illuminate\Notifications\Notifiable;class User extends Authenticatable{    use Notifiable;    /**     * The attributes that are mass assignable.     *     * @var array     */    protected $fillable = [        'role_id', 'name', 'email', 'password',    ];    /**     * The attributes that should be hidden for arrays.     *     * @var array     */    protected $hidden = [        'password', 'remember_token',    ];    /**     * The attributes that should be cast to native types.     *     * @var array     */    protected $casts = [        'email_verified_at' => 'datetime',    ];}醫(yī)院模型namespace App;use Illuminate\Database\Eloquent\Model;class Hospital extends Model{    /**     * @var String $table;     */    protected $table = 'hospitals';    /**     * @var Array $fillable;     */    protected $fillable = [        'user_id', 'companyname', 'adress', 'postalcode', 'residence',        'phonenumber', 'mailadress'    ];}
查看完整描述

1 回答

?
夢(mèng)里花落0921

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊

首先你不需要像這樣在你的動(dòng)作中注入U(xiǎn)ser模型store


public function store(User $user, Request $request)

您可以在hospital控制器的store操作中使用類似以下代碼的事務(wù)


public function store(Request $request)

{

    $request->validate([

        'name' => 'required|max:255',

        'email' => 'required|max:255|unique:users',

        'password' => 'required|min:8',

    ]);


    DB::transaction(function () use($request){

        // $user = User::create($this->transformForm($request)); 


        $user = User::create([

            'name' => $request->input('name'),

            'email' => $request->input('email'),

            'password' => $request->input('password'),

        ]);


        // update or insert here

        Hospital::Create([

            'user_id' => $user->id,

            'companyname' => $request->input('companyname'),

            'adress' => $request->input('adress'),

            'postalcode' => $request->input('postalcode'),

            'residence' => $request->input('residence'),

            // 'phonenumber' => $request->input(), //missing in your form

            // 'mailadress' => $request->input() // missing in your form

        ]);

    });


    return //redirecto to a success page or somewhere else

}


查看完整回答
反對(duì) 回復(fù) 2022-12-30
  • 1 回答
  • 0 關(guān)注
  • 127 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)