簡體   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