Php 在Laravel 5中保存数组
我从一篇ajax文章中发送了一组数据,并试图将它们保存到我的模型中。然而,我得到的是空数组<代码>[]、[]、[]] 编辑 由于下面的评论,我更新了我的模型,现在我得到了我需要的数据 但是,当我向其中添加用户关系时,会返回错误 我将把它附加到末尾,因为这已经很长了 /edit 我将其设置为对数据进行迭代,最初看起来如下所示:Php 在Laravel 5中保存数组,php,laravel,laravel-5,Php,Laravel,Laravel 5,我从一篇ajax文章中发送了一组数据,并试图将它们保存到我的模型中。然而,我得到的是空数组[]、[]、[]] 编辑 由于下面的评论,我更新了我的模型,现在我得到了我需要的数据 但是,当我向其中添加用户关系时,会返回错误 我将把它附加到末尾,因为这已经很长了 /edit 我将其设置为对数据进行迭代,最初看起来如下所示: [{start:1.17,end:1.66,id:“region_ovkb6fg”}, {开始:2.19,结束:2.53,id:“杰雷波地区”}, {开始:3.07,结束:3.53
[{start:1.17,end:1.66,id:“region_ovkb6fg”},
{开始:2.19,结束:2.53,id:“杰雷波地区”},
{开始:3.07,结束: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循环而不运行时,模型将返回上述数组以及正确的信息
我曾尝试在newregion()
方法中使用此循环,但这是不允许的,并且会返回错误
我尝试过使用newregion()->fill($inserts)
,但也不起作用
发生的情况是,newregion
正在为此数组创建一个数组,数据在顶层不再可见<代码>新区域(阵列(阵列(区域id:“wavesurfer_p567v4”,开始:1.88,结束: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
}
]
保存这张桌子 我正在调用
使用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');
}
}
用户模型如下:
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');
}
}
第一个问题是可填充的
和受保护的
属性
而保护的
的默认值是['*']
,这对可填充的
也不起作用。最好明确指定所有属性:
protected $fillable = ['region_id', 'start', 'end'];
除此之外,被保护的是['requion\u id']
,这没有多大意义。属性不能被填充和保护,因为它们是相反的
在这些修复之后,剩下的问题是使用save()
。保存多个模型时,必须使用saveMany()
:
Auth::user()->regions()->saveMany($inserts);
您是否在模型中设置了$filleble
属性?如果您不确定,请发布您的型号代码。嗯。。不知道这是否是问题所在,但您应该在循环之前初始化数组:$inserts=array()
你能把dd($inserts)
的输出贴出来吗?@lukasgeiter我在原来的问题上加了一句,现在$fillable=['*']
不起作用了。您必须指定它们或设置$guarded=[]
以允许所有输入(最好是前者)。此外,您当前的区域\u id
设置为受保护,这也是一个问题。