【发布时间】:2014-12-05 04:21:06
【问题描述】:
我是 laravel 框架的新手,我只是发现很难理解 laravel 形式的可用性......所以这是我面临的一些问题
首先我的下拉列表应该有我的类别名称,在下拉列表的最后,它将是我的特定下拉列表添加新类别,当我选择时将触发我的 javascript...我在这里我的表单代码
{{ Form::select('kategori',KategoriArtikel::lists('name','id'),Input::old('kategori'),array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
这将在我的数据库中显示正确的值以及正确的选择值,但它缺少我的特定下拉列表,所以我尝试像这样添加
<?php
$tambah = array('tambah' => 'Tambah Kategori Baru');
$list = array_merge(KategoriArtikel::lists('name','id'),$tambah);
?>
{{ Form::select('kategori',array(KategoriArtikel::lists('name','id'),$tambah),Input::old('kategori'),array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
但它会生成 2 个具有不同布局(丑陋)并且不显示正确选择的值,所以我尝试了其他方式
<?php
$tambah = array('tambah' => 'Tambah Kategori Baru');
$list = array_merge(KategoriArtikel::lists('name','id'),$tambah);
?>
{{ Form::select('kategori',$list,Input::old('kategori'),array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
它没有生成,我的下拉布局是它应该是的,但现在不再来自我的表 ID,而是序列号 0、1、2、3 等 有什么解决办法吗?
第二个问题是当我提交表单时,它不起作用,我在表中的字段是 int,当我提交它时只是没有保存/更新,所以我尝试 dd(Input::all( )) 并且我的下拉列表正在返回所选选项值的字符串,并且它没有保存到我的表中......那为什么呢?它也不适用于包含 tinyint(1) 的布尔字段...所以基本上所有值都返回字符串。
它返回这个
array(8) { ["_method"]=> string(3) "PUT" ["_token"]=> string(40) "IspUKdCETMe4Nn3pDI43GI7aJKQfXpupJvQAy1k6" ["simpan"]=> string(6) "simpan" ["judul"]=> string(18) "test "kegiatan" 21" ["kategori"]=> string(1) "9" ["kategori_baru"]=> string(0) "" ["status"]=> string(1) "0" ["content"]=> string(1012) "
I used--and I don't put my arm round your waist,' the Duchess said to herself. 'Of the mushroom,' said the Dormouse, and repeated her question. 'Why did they live at the sides of it, and then turned to the tarts on the twelfth?' Alice went on, 'that they'd let Dinah stop in the shade: however, the moment he was obliged to have wondered at this, but at the Caterpillar's making such a capital one for catching mice--oh, I beg your pardon!' cried Alice hastily, afraid that she had read about them in books, and she at once without waiting for turns, quarrelling all the children she knew, who might do something better with the time,' she said to herself; 'I should like to drop the jar for fear of killing somebody, so managed to swallow a morsel of the house, quite forgetting her promise. 'Treacle,' said the Gryphon: and it sat for a few minutes to see if she had grown up,' she said this, she looked down at her as she wandered about for it, you know--' (pointing with his head!' she said,.
" }
如您所见,['kategori'] 应该使我的表中的字段类别为 9,但它并没有改变我的数据库中的任何内容
我的 laravel 很简单,但是在使用数据库时非常混乱(找不到任何与数据库检索数据等相关的文档示例)
编辑:
这是我的控制器
public function update($id)
{
$artikel = Artikel::findOrFail($id);
if(Input::get('simpan')){
//dd(Input::all());
$validator = Validator::make($data = Input::all(), Artikel::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$judul = Input::get('judul');
$artikel->update($data);
return Redirect::route('admin.artikels.index')->with('message', 'Artikel ' .$judul. ' Telah berhasil di ubah.');
}elseif(Input::get('batal')){
return $this->index();
}
}
这是我的看法
@extends('admin._layouts.admin')
@section('content')
{{ Form::model($artikel, array('route' => array('admin.artikels.update',$artikel->id), 'method' => 'put')) }}
<div class="panel panel-default">
<!--button-->
<div class="panel-heading tooltip-demo">
{{ Form::submit('Simpan',array('class' => 'btn btn-primary', 'data-toggle' => 'tooltip',
'data-placement' => 'top','title' => 'Menyimpan artikel' )) }}
{{ Form::submit('Batal',array('class' => 'btn btn-default', 'data-toggle' => 'tooltip',
'data-placement' => 'top','title' => 'Batal menambah artikel dan kembali ke halaman kelola artikel' )) }}
</div>
<!--/button-->
<div class="panel-body">
<!--judul-->
<div class="col-lg-10">
<div class="form-group">
{{ Form::label('Judul Artikel') }}
{{ Form::text('judul',null,array('class' => 'form-control', 'placeholder' => 'Silahkan masukkan judul artikel'))}}
{{ $errors->first('judul', '<p class="error">:message</p>') }}
</div>
</div>
<!--/judul-->
<!--kategori-->
<div class="col-lg-4">
<div class="form-group">
{{ Form::label('Kategori') }}
<?php
$tambah = array('tambah' => 'Tambah Kategori Baru');
$list = array_merge(KategoriArtikel::lists('name','id'),$tambah);
?>
{{ Form::select('kategori',$list,'Pilih Kategori Artikel',array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
</div>
</div>
<!--/kategori-->
<!--kategori baru-->
<div class="col-lg-4" id="pilihan" style="display:none;">
<div class="form-group">
{{ Form::label('Kategori Baru') }}
{{ Form::text('kategori_baru',null,array('class' => 'form-control', 'placeholder' => 'Silahkan masukkan kategori baru',
'maxlength' => '30'))}}
</div>
</div>
<!--/kategori baru-->
<!--status-->
<div class="col-lg-4">
<div class="form-group">
{{ Form::label('Status') }}
{{ Form::select('status',array('0' => 'Tidak diterbikan', '1' => 'Terbitkan'),null, array('class' => 'form-control')) }}
</div>
</div>
<!--/status-->
<!--artikel pilihan-->
<div class="col-lg-5">
<div class="form-group">
{{ Form::label('Artikel Pilihan') }}
<div class="input-group">
<span class="input-group-addon">
{{ Form::checkbox('pilihan','1',true,array('id' => 'artikelpilihan')) }}
</span>
{{ Form::text('null','Tidak',array('class' => 'form-control', 'id' => 'artikeltext' ,'disabled' => 'true'))}}
</div>
</div>
</div>
<!--/artikel pilihan-->
<!--content-->
<div class="col-lg-12">
{{ Form::label('Isi Artikel') }}
{{ Form::textarea('content',null,array('style' => 'height:300px')) }}
{{ $errors->first('content', '<p class="error">:message</p>') }}
</div>
<!--/content-->
</div>
</div>
{{ Form::close() }}
{{ HTML::script('js/tinymce/tinymce.min.js') }}
<script type="text/javascript">
tinymce.init({
selector: "textarea",
theme: "modern",
skin: 'light',
plugins: [
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"emoticons template paste textcolor colorpicker textpattern"
],
toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
toolbar2: "print preview media | forecolor backcolor emoticons | fontselect fontsizeselect",
image_advtab: true,
templates: [
{title: 'Test template 1', content: 'Test 1'},
{title: 'Test template 2', content: 'Test 2'}
],
file_browser_callback: RoxyFileBrowser
});
function RoxyFileBrowser(field_name, url, type, win) {
var roxyFileman = '../../../../public/js/tinymce/plugins/fileman/index.html?integration=tinymce4';
if (roxyFileman.indexOf("?") < 0) {
roxyFileman += "?type=" + type;
}
else {
roxyFileman += "&type=" + type;
}
roxyFileman += '&input=' + field_name + '&value=' + document.getElementById(field_name).value;
tinyMCE.activeEditor.windowManager.open({
file: roxyFileman,
title: 'File Manager',
width: 800,
height: 480,
resizable: "yes",
plugins: "media",
inline: "yes",
close_previous: "no"
}, { window: win, input: field_name });
return false;
}
</script>
@stop
【问题讨论】:
-
你真的不应该在你的视图文件中使用
KategoriArtikel::lists('name','id')。它应该在您的控制器中。你能告诉我们你的控制器和视图吗 -
控制器中的
return $this->index();是否会将您带到您发布代码的视图文件? -
是的,那是我的取消/batal按钮,所以它与保存在 Input::get('simpan') 中捕获的数据无关,它将更新我的其他输入,除了下拉列表,所以像 judul 和内容将被更新,但像 categori、pilihan、status 和很快这样的下拉列表不会更新....
标签: php forms laravel laravel-4