[英]How to assign imploded value from DB into Laravel blade template view?
我已经将我所有的复选框内爆为来自spoken_language[]
的 1 个值,包括 1 个文本字段值。
html
<div>
{{ Form::checkbox('spoken_language[]', "English", array('class' => 'form-check-input')) }}
<label>English</label>
</div>
<div>
{{ Form::checkbox('spoken_language[]', "Bahasa Melayu", array('class' => 'form-check-input')) }}
<label>Bahasa Melayu</label>
</div>
<div>
{{ Form::checkbox('spoken_language[]', null, array('class' => 'form-check-input')) }}
<label>Others</label>
{{ Form::text('spoken_language[]', null, array('class' => 'form-control')) }}
</div>
php
$request->merge(['spoken_language' => implode(',', (array) $request->get('spoken_language'))]);
问题是如何根据 1 个值将数据绑定到刀片模板视图(复选框和文本字段)?
我的收藏
<?php
$user = DB::table('users')
->where("users.id", Auth::user()->id)
->first();
return View::make('user.profile')->with('user', $user);
像这个例子一样,我得到了这些值English,Bahasa Melayu,Urdu
,然后我需要在文本字段中为每个复选框和 Urdu 分配。 但如果是English,Bahasa Melayu
,只是复选框而已。 Urdu
也是如此,进入文本字段。
假设$storedSpokenLang
具有在数据库中检索到的值。
您可以创建用户已保存语言的集合并将其传递给刀片视图模板。
$spokenLang = collect(explode(',', $storedSpokenLang))->flip();
在视图中,可以从这里开始:
<div>
{{ Form::checkbox(
'spoken_language[]',
"English",
$spokenLang->has('English'),
array('class' => 'form-check-input')) }}
<label>English</label>
</div>
<div>
{{ Form::checkbox(
'spoken_language[]',
"Bahasa Melayu",
$spokenLang->has('Bahasa Melayu'),
array('class' => 'form-check-input')) }}
<label>Bahasa Melayu</label>
</div>
<div>
{{ Form::checkbox(
'spoken_language[]',
$spokenLang->except("English", "Bahasa Melayu", "")->isNotEmpty(),
array('class' => 'form-check-input')) }}
<label>Others</label>
{{ Form::text(
'spoken_language[]',
$spokenLang->except("English", "Bahasa Melayu", "")->keys()->implode(','),
array('class' => 'form-control')) }}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.