我想运行以下查询
INSERT INTO static_pages (`page_name`, `title`, `page_display_order`) SELECT 'test','test', MAX(page_display_order)+1 FROM static_pages;
如何使用laravel eloquent执行此查询?这可能吗?
更新
谢谢@Joseph Silber
我使用了这个,就像
一样$staticpage->page_display_order = DB::raw("($subquery)");
$staticpage->save();
这有助于获得最后插入的ID。
答案 0 :(得分:1)
将DB::raw()
与子查询一起使用:
$subquery = DB::table((new StaticPage)->getTable() . ' as page_alias')
->selectRaw('page_display_order + 1 as pdo')
->orderBy('page_display_order', 'desc')
->take(1)->toSql();
StaticPage::create([
'page_name' => 'test',
'title' => 'test',
'page_display_order' => DB::raw("($subquery)"),
]);