繁体   English   中英

在Laravel 5中保存数组的数组

[英]Saving an array of arrays in Laravel 5

我正在从ajax POST发送数据数组,并尝试将它们保存到模型中。 但是我得到的是空数组。 [ [],[],[] ]

编辑

感谢下面的评论,我更新了模型,现在可以获取所需的数据。

但是,当我在其中添加用户关系时,会带来错误。

我将其附加到最后,因为这已经很长了

/编辑

我设置它来遍历原始看起来像这样的数据:

[{start: 1.17, end: 1.66, id: "region_ovkb6fg"}, {start: 2.19, end: 2.53, id: "region_jereppo"}, {start: 3.07, end: 3.53, id: "region_scpnvmo"}]

这是我在控制器中正在做的事情:

public function store ( Request $request)
  {
    if($request->ajax()) {

        $data = $request->all();

        foreach ( $data as $v ) {

          // Region is the name of the Model and is included at the top of the page
          $inserts[] = new Region( array('region_id'=> $v['id'], 'start' => $v['start'], 'end' => $v['end'])); 
        }
    return $inserts;

    // Then I also need to add user_id reference 
   }
 }

没有 Model的情况下运行上述foreach循环将返回上述带有正确信息的数组。

我已经使用这个循环尝试new Region()方法,但是这是不允许的,并返回错误。

我尝试使用new Region()->fill($inserts)但这也没有用。

发生的情况是, new Region正在为此数组创建一个数组,并且数据在顶层不再可见。 new Region(array(array(region_id: "wavesurfer_p567v4", start: 1.88, end: 2.59)))

没有模型的 dd($inserts)只是循环:

array:3 [
  0 => array:3 [
    "region_id" => "wavesurfer_3uc44qg"
    "start" => 2.16
    "end" => 3.09
  ]
  1 => array:3 [
    "region_id" => "wavesurfer_apr1k9"
    "start" => 3.72
    "end" => 4.46
  ]
  2 => array:3 [
    "region_id" => "wavesurfer_edihseo"
    "start" => 5.13
    "end" => 6
  ]
]

dd($inserts) 模型,使用上面的代码:

    array:2 [
  0 => Region {#156
    #table: "regions"
    #fillable: array:4 [
      0 => "region_id"
      1 => "start"
      2 => "end"
      3 => "data"
    ]
    #connection: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:3 [
      "region_id" => "wavesurfer_dj3g1g8"
      "start" => 2.54
      "end" => 3.14
    ]
    #original: []
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [
      0 => "*"
    ]
    #dates: []
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: false
  }
  1 => Region {#158
    #table: "regions"
    #fillable: array:4 [
      0 => "region_id"
      1 => "start"
      2 => "end"
      3 => "data"
    ]
    #connection: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:3 [
      "region_id" => "wavesurfer_808mkm"
      "start" => 3.68
      "end" => 4.14
    ]
    #original: []
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [
      0 => "*"
    ]
    #dates: []
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: false
  }
]

保存此表。

我正在呼叫use Auth; 在控制器的顶部并添加以下内容: Auth::user()->regions()->save($inserts); 导致此错误:

ErrorException in HasOneOrMany.php line 165:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, array given, called in /home/vagrant/Code/HSAnnotator/app/Http/Controllers/WaveController.php on line 45 and defined

我的区域模型如下所示:

class Region extends Model {
    protected $fillable = ['region_id','start', 'end', 'data'];
    public function user ()
    {
        return $this->belongsTo('App\User', 'user_id');
    }
}

和用户模型是这样的:

    class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

  /* user related code for passwords and built in things*/


        public function regions ()
        {
            return $this->hasMany('App\Region');
        }
        public function markers ()
        {
            return $this->hasMany('App\Marker');
        }

    }

第一个问题是fillable和受guarded属性。

虽然guarded的默认值为['*'] ,但它对于fillable也不起作用。 最好明确指定所有属性:

protected $fillable = ['region_id', 'start', 'end'];

除了受guarded['reqion_id'] ,这没有什么意义。 由于属性相反,因此无法填充和保护属性。


这些修复之后,剩下的问题就是使用save() 保存多个模型时,必须使用saveMany()

Auth::user()->regions()->saveMany($inserts);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM