在laravel中保存一对多关系时的非法偏移类型

时间:2017-08-07 11:29:33

标签: php laravel laravel-5.4

我有一个Package这样的模型:

class Package extends Model
    {

        protected $primaryKey = 'package_id';
        protected $fillable   = ['title', 'slug', 'desc'];

        public function options ()
        {
            return $this->hasMany('App\Option', 'package_id', 'package_id');
        }
    }

这样的Option模型:

class Option extends Model
    {
        public    $timestamps = false;
        protected $primaryKey = ['option_id'];
        protected $fillable   = ['package_id','text'];

        public function package ()
        {
            return $this->belongsTo('App\Package', 'package_id', 'package_id');
        }
    }

正如您所看到的,它们之间存在一对多关系。

现在我想为新创建的包保存一些新选项。因为我写了这个控制器方法:

public function store (StorePackageRequest $request)
        {

            $data    = $request->except('options');

            $package = Package::create($data);

            $options = $request->get('options');

            $opts = [];
            foreach ($options as $key => $option) {
                $opts[] = new Option(['text' => $option]);
            }

            $package->options()->saveMany($opts);

            $result = ['success' => true, 'generated_id' => $package->package_id];

            return $result;
        }
来自options

$request->get('options')参数是这样的输入元素:

<input type="text" name="options[]" class="form-control">

但是在foreach循环的第一次旋转中我得到了这个错误:

ErrorException in HasAttributes.php line 818:
Illegal offset type

有什么问题?

更新:

完全错误:

[2017-08-07 16:49:50] local.ERROR: exception 'ErrorException' with message 'Illegal offset type' in D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php:818
Stack trace:
#0 D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(818): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Illegal offset ...', 'D:\\wamp\\www\\bra...', 818, Array)
#1 D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(803): Illuminate\Database\Eloquent\Model->getCasts()
#2 D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(832): Illuminate\Database\Eloquent\Model->hasCast('text', Array)
#3 D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(565): Illuminate\Database\Eloquent\Model->isDateCastable('text')
#4 D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(525): Illuminate\Database\Eloquent\Model->isDateAttribute('text')
#5 D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(223): Illuminate\Database\Eloquent\Model->setAttribute('text', 'a')
#6 D:\wamp\www\brands\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(145): Illuminate\Database\Eloquent\Model->fill(Array)

2 个答案:

答案 0 :(得分:2)

我认为您遇到此问题的原因是因为您将$primarykey设置为Option模型中的数组。

尝试将其更改为:

protected $primaryKey = 'option_id';

希望这有帮助!

答案 1 :(得分:0)

在阅读API后,get()不存在Request方法。

而不是$request->get('options'),请尝试$request->input('options')