Laravel. SQL server вставка записей с ID

In Connection.php line 664:

  SQLSTATE[23000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
  Cannot insert explicit value for identity column in table 'october_test_pages' when 
  IDENTITY_INSERT is set to OFF. (SQL: insert into [october_test_pages] ([id], [type], [content]) 
  values (1, 1, {"title":"This is a simple page","content":"<h1>Hello, World<\/h1>"}))
// seed_tables
...
DB::unprepared('SET IDENTITY_INSERT october_test_pages ON');
foreach($pages as $page) {
    Page::create($page);
}
DB::unprepared('SET IDENTITY_INSERT october_test_pages OFF');
...

Похожие записи

Laravel. Catch FindOrFail Exception

use Illuminate\Database\Eloquent\ModelNotFoundException;
...
try {
  $publication = Publication::findOrFail($id);
} catch (ModelNotFoundException $ex) {
  // Error handling code
}
21 декабря 2017 г. в Laravel

Про сохранение полей даты в Eloquent

Если в моделе не указать

protected $dates = ['something_at'];

То надо делать формат

// Поле типа date 
'something_at' => Carbon::now()->addDays(7)->format('Y-m-d'),

// Поле типа date 
'something_at' => Carbon::now()->addDays(7)->format('Y-m-d H:i:s'),

Eloquent collection groupBy Carbon

use Carbon\Carbon;
...

$projectsGroupedByYear = $projects->groupBy(function($p) {
    return Carbon::parse($p->start_year_date)->format('Y');
});