亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 mysql 數據庫中的 laravel 中保存多個輸入?

如何在 mysql 數據庫中的 laravel 中保存多個輸入?

PHP
智慧大石 2022-01-14 17:13:14
我在 laravel 中有一個票務系統,我的問題是我的網站中有多個輸入字段,這些字段title fname lasname來自包含多少乘客的輸入?,F在我有兩個表,它們是booking和booking_details。我想將聯系人詳細信息和手機號碼(如下所示)保存到表中bookings,以及引用. 我已經完成了數據庫和兩個表的關系的設置,現在我的問題是在我的 booking_detailsbooking_idbooking_details桌子?那是我唯一的問題。我嘗試了不同的方法,比如使用數組,但我無法解決它。也許語法或邏輯是錯誤的。有人可以告訴我該怎么做嗎?提前致謝。我的表格。 <form method="POST" action="{{url("/saveBooking")}}">                        <div class="form-row">                            <div class="col-sm-6 form-group">                                <input name="email" class="form-control" id="email" required="" placeholder="Enter Email" type="text">                            </div>                            <div class="col-sm-6 form-group">                                <input name="mobile_no" class="form-control" data-bv-field="number" id="mobileNumber" required="" placeholder="Enter Mobile Number" type="text">                            </div>                        </div>                        <p class="text-info">Your booking details will be sent to this email address and mobile number.</p>                            @for ($i = 0 ; $i<$split1 ; $i++)                            <button style="margin: 5px;" type="button" class="btn btn-sm btn-info collapsible" >Passenger <?php echo $i + 1 ?></button>                            <div class="form-row content"  style=" display: none;margin-top:7px;" >                                <div class="col-sm-2 form-group" >                                    <select class="custom-select" id="title" name="title[]" required="">                                        <option value="">Title</option>                                        <option>Mr</option>                                        <option>Ms</option>                                        <option>Mrs</option>                                    </select>                                </div>
查看完整描述

2 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您的數據來自表單的數組。因此,循環遍歷數組并在每次迭代中插入值。示例方法:


public function addBooking(Request $request)

{

    $booking = Booking::create([

        'mobile_no' => $request->mobile_no,

        'email' => $request->email,

        //add if you want you add any more column

    ]);


    foreach ($request->title as $key => $value) {

        BookingDetails::create([

            'booking_id' => $booking_id,

            'title' => $request->title[$key],

            'fname' => $request->fname[$key],

            'lname' => $request->lname[$key],

            //other columns

        ]);

    }


    return->back();

}


查看完整回答
反對 回復 2022-01-14
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

公共函數 addBooking(Request $request){


    $booking = Booking::create([

        'mobile_no' => $request->mobile_no,

        'email' => $request->email,

        //add if you want you add any more column

    ]);

$bookings=[];


    foreach($request->title as $key => $value){

        array_push(bookings,[

            'booking_id' => $booking_id,

            'title' => $request->title[$key],

            'fname' => $request->fname[$key],

            'lname' => $request->lname[$key]

        ]);

    }

   BookingDetails::insert($bookings);



    return->back();

}


查看完整回答
反對 回復 2022-01-14
  • 2 回答
  • 0 關注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號