【问题标题】:Multiple select insert data using pivot table Laravel error preg_replace():使用数据透视表 Laravel 错误 preg_replace() 多选插入数据:
【发布时间】: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


【解决方案1】:
$call->purposes()->attach(['field_name'=> $request->input('purposes')]);
       // return Redirect::to('/calls');

field_name 替换为用途表的列名

【讨论】:

  • 嗨,我将此数组保存到数据透视表中,您可以查看我的问题
  • 试试这个调用->目的()->attach([$request->input('目的')]);
  • @monash 视频中出现同样的错误兄弟,一切正常,但当我尝试插入步骤时出现错误
猜你喜欢
  • 1970-01-01
  • 2020-09-19
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
相关资源
最近更新 更多