Laravel 4:在laravel中插入max + 1

时间:2014-11-07 13:55:44

标签: laravel insert max eloquent

我想运行以下查询

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。

1 个答案:

答案 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)"),
]);