Laravel 有许多旧的输入
我有一个模特类别。类别有许多本地化。当我存储类别时,我有以下输入:Laravel 有许多旧的输入,laravel,laravel-4,Laravel,Laravel 4,我有一个模特类别。类别有许多本地化。当我存储类别时,我有以下输入: {{ Form::text('title[en]', Input::old('title')) }} {{ Form::text('title[ru]', Input::old('title')) }} 我将其存储在我的控制器中: // Gett all inputs $inputs = Input::all(); // Create resource $ite
{{ Form::text('title[en]', Input::old('title')) }}
{{ Form::text('title[ru]', Input::old('title')) }}
我将其存储在我的控制器中:
// Gett all inputs
$inputs = Input::all();
// Create resource
$item = Category::create([]);
// Create localization
foreach(Input::get('title') as $locale => $title)
{
$locale = new Localization(['locale' => $locale, 'title' => $title]);
$locale = $item->localization()->save($locale);
}
这很有效,但更新这种关系的最佳实践是什么?目前,我正在尝试使用Form::model绑定
@foreach($locales as $key => $locale)
{{ Form::text('title['.$locale.']', $model->translate($locale)->title, ['class' => 'form-control']) }}
@endforeach
我不知道Input::old在这种情况下如何工作,所以现在我使用$model->translate($locale)->title来获得正确的值。基本上,更新/验证部分实际上不起作用。您可以建议如何更改以验证这种关系并对其进行更新?今天,我发现了一个使用验证存储/更新关系的有效解决方案。我希望这是最好/最简单的方法。我用输入创建了一个新数组进行验证,并在视图中相应地更改了错误 这是我的更新控制器
public function update($id)
{
// Find resource
$item = Category::find($id);
foreach(Input::get('title') as $locale => $title)
{
$v['title_'.$locale] = $title;
}
// Attempt validation
if($item->validate($v))
{
foreach(Input::get('title') as $locale => $title)
{
$localization = $item->translate($locale);
$localization->title = $title;
$localization->save();
}
return Redirect::action('AdminCategoryController@edit', [$item->id]);
}
else
{
// Failure, get errors
$errors = $item->errors();
return Redirect::back()
->withInput()
->with('errors', $errors);
}
}
这是更新视图
{{ Form::model($model, ['action' => ['AdminCategoryController@update', $model->id], 'method' => 'PUT']) }}
@foreach($locales as $key => $locale)
<div id="{{ $locale }}">
<div class="form-group">
{{ Form::label('title['.$locale.']', _('admin.title_'.$locale)) }}
{{ Form::text('title['.$locale.']', $model->translate($locale)->title, ['class' => 'form-control']) }}
@if($errors->has('title_'.$locale))
<div class="help-block alert alert-danger">{{ $errors->first('title_'.$locale) }}</div>
@endif
</div>
</div>
@endforeach
{{ Form::close() }}
{{Form::model($model,['action'=>AdminCategoryController@update“,$model->id],'method'=>'PUT'])}
@foreach($key=>$locale的区域设置)
{{Form::label('title['.$locale.]',{('admin.title.$locale))}
{Form::text('title['.$locale.']',$model->translate($locale)->title,['class'=>'Form control'])}
@如果($errors->has('title.'$locale))
{{$errors->first('title.'$locale)}
@恩迪夫
@endforeach
{{Form::close()}}
通过这种方式,您可以轻松地对Laravel中的所有类型的关系(输入数组)进行CRUD、验证