【发布时间】:2016-08-18 12:58:17
【问题描述】:
我按照 Jeffry 方式教程完成了本教程中的所有操作LInK 当我从我的商店功能插入数据时,我收到如下错误:
ErrorException in helpers.php line 747:
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
in helpers.php line 747
at HandleExceptions->handleError('2', 'preg_replace(): Parameter mismatch, pattern is a string while replacement is an array', 'C:\xampp\htdocs\elixir\vendor\laravel\framework\src\Illuminate\Support\helpers.php', '747', array('search' => '\?', 'replace' => array('1', '', '1', '1', '', '1', '', array('1', '2'), '', '', '', '', '', '', '', '0', '', '', '1', '2016-08-18 18:15:46', '2016-08-18 18:15:46'), 'subject' => 'insert into `psl_calls` (`branch_id`, `vessel_name`, `port_id`, `jetty_id`, `voyage_no`, `principal_id`, `reference_no`, `purposes`, `eta_estimate_time`, `etb_estimate_time`, `etc_estimate_time`, `etd_estimate_time`, `previous_port_id`, `next_port_id`, `employee_id`, `quality_rating`, `cancel_remark`, `no_cost_time_remark`, `user_id`, `updated_at`, `created_at`) values (1, , 1, 1, , 1, , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', 'value' => array('1', '2')))
我的控制器功能是这样的:
public function store(Request $request)
{
$input = $request->all();
$input['user_id'] = \Auth::id();
$call = PslCall::create($input);
$call->purposes()->attach($request->input('purposes'));
// return Redirect::to('/calls');
}
我的 PslCall 模型:
public function purposes()
{
return $this->belongsToMany('App\Models\PslPurpose')->withTimestamps();
}
我有一个数据透视表调用:
Schema::create('psl_call_psl_purpose', function (Blueprint $table) {
$table->integer('psl_call_id')->unsigned()->index();;
$table->foreign('psl_call_id')->references('id')->on('psl_calls')->onDelete('cascade');
$table->integer('psl_purpose_id')->unsigned()->index();;
$table->foreign('psl_purpose_id')->references('id')->on('psl_purposes')->onDelete('cascade');
$table->timestamps();
});
【问题讨论】:
-
试试
$request->get('purposes')insead。 -
关于
preg_replace的问题,但没有关于preg_replace的问题? -
在 laravel 中,我们可以将数组传递给数据透视表,例如 `$call->purposes()->attach($request->input('purposes'));`
标签: laravel eloquent laravel-5.2 pivot-table