I am trying laravel eloquent for sql insert into select query which is as follows-
$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. I need to do this using laravel eloquent but can not find any reliable method.
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). 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();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.