php - Laravel 4 - How to insert multiple models? -
php - Laravel 4 - How to insert multiple models? -
im trying insert multiple models database no luck... have form (blade) looks this:
@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 in controller, have: (used array_filter clear unset values if user filled in 2 properties)
$boro = array_filter(input::get('boro')); $house_num = array_filter(input::get('house_num')); $street = array_filter(input::get('street')); now i'm stuck... can't figure out how loop thru 3 arrays , save multiple models (rows).
note: in user model did set hasmany properties relationship.
my property model looks like: id user_id boro housenum street
so if in arrays have 2 addresses, need rows.
any ideas?
// input values $boros = input::get('boro'); $numbers = input::get('house_num'); $streets = input::get('street'); // iterate through arrays $models = array(); $total = count($boros); for( $c=0; $c < $total; $c++ ) { $models[] = array('boro' => $boros[$c], 'numbers' => $numbers[$c], 'street' => $streets[$c]); } // insert models db::table('whatever')->insert($models);
php mysql laravel laravel-4
Comments
Post a Comment