我从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
}
}
运行上面的foreach循环而不模型将返回上面的数组,其中包含正确的信息。
我已尝试在<{em>} 方法中使用此循环但不允许这样做并返回错误。
我已尝试使用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');
}
}
答案 0 :(得分:1)
fillable
和guarded
属性的第一个问题。
虽然guarded
的默认值为['*']
,但对fillable
也不起作用。最好明确指定所有属性:
protected $fillable = ['region_id', 'start', 'end'];
除了guarded
['reqion_id']
之外,save()
没有多大意义。由于属性相反,属性不能被填充和保护。
在修复之后,剩下的问题是saveMany()
的使用。保存多个模型时,必须使用Auth::user()->regions()->saveMany($inserts);
:
$("#people_list").getKendoDropDownList().dataSource.insert({
person_id: "John",
name: "John"
})