[英]Laravel eloquent insert into select
我正在嘗試將 laravel eloquent 用於 sql 插入 Z99938282F04071859941E18F16EFCF2 查詢如下
$refId='111';
$newStatus='S2';
INSERT INTO parcel(items, status) SELECT items,'$newStatus' FROM parcel where ref_id ='$refId' LIMIT 1;
此查詢使用newStatus變量插入到我的數據庫的包裹表中,其中從數據庫中選擇所需的某些項目。 我需要使用 laravel eloquent 來執行此操作,但找不到任何可靠的方法。
提前致謝。
// First Create Model Parcel and FInd Your required row with id
// Below code use for selecting data from database.
$parcel = Parcel::query()->where('database column name', '=', $parcel_id)->first()
// If you want to store data in table use below code.
$parcel = new Parcel();
$percel->items = $item;
$percel->status = $newStatus;
$percel->save();
您可以使用 insertUsing(自 Laravel 5.17 起)。 您的代碼將僅在一條指令中設置:
DB::table('parcel')->insertUsing(['items', 'status'],
function ($request) use ($newStatus, $refId) {
$request->select('items', DB::raw("$newStatus"))
->from('parcel')
->where('ref_id', '=', $refId)
->first()
->toSql();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.