[英]Laravel eloquent insert into select
I am trying laravel eloquent for sql insert into select query which is as follows-我正在尝试将 laravel eloquent 用于 sql 插入 Z99938282F04071859941E18F16EFCF2 查询如下
$refId='111';
$newStatus='S2';
INSERT INTO parcel(items, status) SELECT items,'$newStatus' FROM parcel where ref_id ='$refId' LIMIT 1;
This query does insertion into parcel table of my database with the newStatus variable where selecting certain items needed from the db.此查询使用newStatus变量插入到我的数据库的包裹表中,其中从数据库中选择所需的某些项目。 I need to do this using laravel eloquent but can not find any reliable method.我需要使用 laravel eloquent 来执行此操作,但找不到任何可靠的方法。
Thanks in advance.提前致谢。
// 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();
You can use insertUsing (since Laravel 5.17).您可以使用 insertUsing(自 Laravel 5.17 起)。 Your code will be set in only one instruction:您的代码将仅在一条指令中设置:
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.