LoginSignup
2
1

More than 5 years have passed since last update.

laravelでfirstOrNewしたら、preg_replace(): Parameter mismatch, pattern is a string while replacement is an arrayと怒られた

Posted at

laravel4を使用しているプロジェクトでタイトルのようなことがあったときに行った対応をメモ。
※laravel5ではどうなっているかは確認していません。

状況

以下のようなソースコードを実行したときに、エラーが発生しました

$conditions = [
    'id' => $id,
];

$data = TestModel::firstOrNew($conditions);

$data['name'] = 'test';

$data->save();

何の変哲もない処理ですが、これを実行するとこんなstacktraceが、、、

local.ERROR: exception 'ErrorException' with message 'preg_replace():
Parameter mismatch, pattern is a string while replacement is an array'
in ~/vendor/laravel/framework/src/Illuminate/Support/helpers.php:887

原因

TestModelのコンストラクタでフィールドに変数を追加していたことが原因でした。
どうやらカラムと認識されていたようです。

TestModel.php
<?php
class TestModel extends Illuminate\Database\Eloquent\Model
{
    public function __construct()
    {
         parent::__construct();

         // こいつが原因
         $this->hoge = ['aaa', 'bbb'];
    }
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1