gpt4 book ai didi

laravel - 如何在 Laravel5 中使用 Eloquent 在数据库中插入多行

转载 作者:行者123 更新时间:2023-12-02 04:16:09 24 4
gpt4 key购买 nike

我的 Controller 看起来像这样

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Middleware\Role;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\PaymentRequest;
use App\User;
use App\Invoice;
use App\comments;
use Session;
use Validator;


class CollectionController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/

public function __construct(){

$this->middleware('role:collector'); // replace 'collector' with whatever role you need.
}


public function postPayment(PaymentRequest $request){

$insertPayment=Input::get();

$payment= new paymentrecieved();
$comment= new comments();

$payment->invoice_id=$insertPayment['invoiceid'];
$payment->recieved_amount=$insertPayment['recieved_amount'];
$payment->refno=$insertPayment['ref_no'];
$payment->date=$insertPayment['date'];

if($insertPayment['adjustmentmode']=='Option')
$payment->adjust_mode='NONE';
else
$payment->adjust_mode=$insertPayment['adjustmentmode'];
$payment->save();

$request->session()->flash('alert-success', 'Payment Has Been inserted Successfully');
return redirect('collection/payment/1');

//dd($insertPayment);
}




}

因此,在我的表单中,与付款相关的详细信息位于一个数组中,我想知道如何创建多行?并将其全部保存一次。

感谢和问候

最佳答案

试试这个,

$result = Banner::create([
'name' => $name,
'status' => $status,
'description' => $description,
'category_id' => $category_id,
'ordering' => $ordering,
'link' => $link,
]);
$result = Banner::create($data);

您可以传递多维数组,在这种情况下您必须使用insert

for(..){
$data[] = [
'name' => $name,
'status' => $status,
'description' => $description,
'category_id' => $category_id,
'ordering' => $ordering,
'link' => $link,
];
}

$result = Banner::insert($data)

注意:这不会更新/创建时间戳,因此您也必须使用插入数组传递它。

希望有帮助..

关于laravel - 如何在 Laravel5 中使用 Eloquent 在数据库中插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33627259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com