【问题标题】:Laravel eloquent insert into selectLaravel 雄辩插入选择
【发布时间】:2020-07-29 12:38:21
【问题描述】:

我正在尝试 laravel eloquent for sql insert into select 查询,如下-

$refId='111';
$newStatus='S2';

INSERT INTO parcel(items, status) SELECT items,'$newStatus' FROM parcel where ref_id ='$refId' LIMIT 1;

此查询使用 newStatus 变量插入到我的数据库的 parcel 表中,其中从数据库中选择所需的某些项目。我需要使用 laravel eloquent 来做到这一点,但找不到任何可靠的方法。

提前致谢。

【问题讨论】:

  • 您是否尝试过将您的查询翻译成 Eloquent?

标签: php mysql laravel select insert


【解决方案1】:
// 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();

【讨论】:

  • 感谢您的建议。在只修改了几个列值并让现有值与以前相同之后,我试图将我的 Parcel 表的一行中的相同数据插入到一个新数据中。如需更多说明,我认为这可能会有所帮助-w3schools.com/sql/sql_insert_into_select.asp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 2013-08-23
相关资源
最近更新 更多