Php Laravel 4-如何插入多个模型?

Php Laravel 4-如何插入多个模型?,php,mysql,laravel,laravel-4,Php,Mysql,Laravel,Laravel 4,我试图插入多个模型到数据库中,但没有运气。。。 我有一个形状(刀片)看起来像这样: @extends('layouts.properties') @section('content') {{ Form::open(['route' => 'properties.store', 'class' => 'uk-form', 'id' => 'properties-form']) }} @for ($i = 0; $i < Auth::user()->propert

我试图插入多个模型到数据库中,但没有运气。。。 我有一个形状(刀片)看起来像这样:

@extends('layouts.properties')

@section('content')

{{ Form::open(['route' => 'properties.store', 'class' => 'uk-form', 'id' => 'properties-form']) }}

@for ($i = 0; $i < Auth::user()->properties_count; $i++)

<fieldset class="uk-margin-large-top property-field-{{ $i + 1 }}">

{{ Form::select('boro[]', ['Pick Borough', 'Manhattan', 'Bronx', 'Brooklyn', 'Queens', 'Staten Island']) }}

{{ Form::text('house_num[]', Input::old('house_num'), ['class' => 'uk-form-width-small', 'placeholder' => 'House Num']) }}

{{ Form::text('street[]', Input::old('street'), ['placeholder' => 'Street']) }}

</fieldset>

@endfor

{{ Form::submit('Save Properties', ['class' => 'uk-button uk-button-primary']) }}

{{ Form::close() }}

@stop
现在我被卡住了。。。无法理解如何循环3个阵列并将其保存到多个模型(行)

注意:在我的用户模型中,我确实设置了hasMany属性关系

我的属性模型如下所示: 身份证件 用户id 波罗 豪斯纳姆 街头

因此,如果在数组中我有2个地址,我需要删除行

有什么想法吗?

//获取您的输入值
// Get your input values
$boros = Input::get('boro');
$numbers = Input::get('house_num');
$streets = Input::get('street');

// Iterate through the arrays
$models = array();
$total = count($boros);
for( $c=0; $c < $total; $c++ ) {
  $models[] = array('boro' => $boros[$c], 'numbers' => $numbers[$c], 'street' => $streets[$c]);
}

// Insert all the models
DB::table('whatever')->insert($models);
$boros=Input::get('boro'); $numbers=输入::get('house_num'); $streets=Input::get('street'); //遍历数组 $models=array(); $total=计数($boros); 对于($c=0;$c<$total;$c++){ $models[]=array('boro'=>$boros[$c],'number'=>$number[$c],'street'=>$streets[$c]); } //插入所有模型 DB::table('whatever')->insert($models);
我不太理解你的问题。你能具体说明你的模型是什么样子,你想插入哪些数据吗?我已经编辑了原始帖子,希望现在已经清楚了…请更具体地说明你的模型,保存的值是某种关系?无意冒犯,但代码有点不可读。那么到底是什么问题呢?我不知道我能比这更清楚。。。
// Get your input values
$boros = Input::get('boro');
$numbers = Input::get('house_num');
$streets = Input::get('street');

// Iterate through the arrays
$models = array();
$total = count($boros);
for( $c=0; $c < $total; $c++ ) {
  $models[] = array('boro' => $boros[$c], 'numbers' => $numbers[$c], 'street' => $streets[$c]);
}

// Insert all the models
DB::table('whatever')->insert($models);
$boros = Input::get('boro');
$streets = Input::get('street');
$numbers = Input::get('house_num');

$properties = [];

foreach ($boros as $key => $boro)
{
   $properties[] = [
     'boro' => $boro, 
     'street' => $streets[$key], 
     'housenum' => $numbers[$key],
     'user_id' => $userId   // probably needed too
   ]
}

// cleaning (and validation maybe?)
$properties = array_map(function ($property) {
    return array_filter($property);
}, $properties);


DB::table('properties')->insert($properties);