php - Laravel 5 form model binding with hasMany -
i have 2 models: category
, categoryvalue
.
inside category
model have:
public function values() { return $this->hasmany('app\categoryvalue'); }
inside categoryvalue
have:
function category() { return $this->belongsto('app\category'); }
in edit view, have form model binding so:
{!! form::model($category, ['route' => ['category.update', $category->slug], 'method' => 'patch']) !!} @include('partials.categoryform') {!! form::close() !!}
where $category
chosen category
model.
the categoryform partial looks like:
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}"> {!! form::label('name', 'name') !!} {!! form::text('name', null, ['class' => 'form-control']) !!} {!! $errors->first('name', '<span class="help-block">:message</span>') !!} </div> <div class="form-group {{ $errors->has('slug') ? 'has-error' : '' }}"> {!! form::label('slug', 'slug') !!} {!! form::text('slug', null, ['class' => 'form-control']) !!} {!! $errors->first('slug', '<span class="help-block">:message</span>') !!} </div> <div class="form-group"> {!! form::submit('save page', ['class' => 'btn btn-primary']) !!} </div>
that's working, when editing category, i'd able edit (add/remove) associate categoryvalue
s. start, want items returned $category->values
inside textarea
each categoryvalue
on new line.
i can think of couple of ways this: use @foreach
, generate textarea
, or popular variable , use in binding. i'm not sure of these, if either, "laravel" way it. i'm not sure if either of can used update records in db properly. how should done?
Comments
Post a Comment