2014-10-10 65 views
0

laravel下拉我是一個新手,laravel框架,我只是覺得它是如此難以瞭解laravel形式的可用性....所以這裏的一些問題,我面臨到與數據庫

首先我的下拉應該是具有我的類別名稱,並在下拉菜單的最後這將是我的具體下拉列表添加新的類別,當我選擇會觸發我的JavaScript ...和IM這裏是我的表單代碼

{{ Form::select('kategori',KategoriArtikel::lists('name','id'),Input::old('kategori'),array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }} 

,將顯示正確的價值從我的數據庫也有正確的選定值,但它缺少我的具體下拉,所以我試圖添加l IKE在此

<?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)')) }} 

,它不產生和我的下拉布局是什麼它應該是,但現在是不是從我的表編號了,但只是序列號0,1,2,3等 任何解決方案呢?

第二個問題是當我提交我的表單,它不工作,我在表中的字段是int,當我提交它只是不保存/更新,所以我試圖dd(輸入::所有())和我的下拉是返回選定的選項值的字符串,它沒有保存到我的表......那麼爲什麼呢?它也不適用於包含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,. 

" } 

,正如你所看到[「駕駛員學校」]是應該使我場駕駛員學校在我的表是9,但它不是我的數據庫進行任何更改

嗯,我laravel簡單,但是當使用數據庫(找不到有關databas檢索數據等任何文件爲例)

編輯這是非常令人困惑:

這是我的控制器

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 
+0

你真的不應該使用'KategoriArtikel ::列表( '名稱', 'ID')'在視圖文件。它應該在你的控制器中。你可以告訴我們你的控制器和視圖 – 2014-10-10 04:40:26

+0

是否'返回$ this-> index();'在你的控制器把你帶到你發佈代碼的視圖文件? – 2014-10-10 05:30:22

+0

是的,這是我的取消/ batal按鈕,所以它與保存在Input :: get('simpan')中捕獲的數據無關,該數據將更新除下拉菜單之外的其他輸入,所以猶如judul和content一樣更新但像kategori,pilihan,狀態,並很快不會更新.... – PUCUK 2014-10-10 05:54:19

回答

0

如果我明白正確的問題,要填充基於從數據庫中值的下拉列表,也想顯示「在類別下拉菜單中選擇一個類別「。目前您的下拉值不會被保存。

要填充的下拉把下面的代碼你的控制器的方法,通過它您加載的視圖文件。

$data['kategori'] = KategoriArtikel::lists('name','id'); 
$data['kategori'][''] = 'Select a kategori';   
return View::make('YOUR_VIEW_FILE', $data); 

在你的視圖文件,簡單的使用以下,(規則同樣適用於其他選擇下拉菜單):

{{ Form::select('kategori', $kategori , Input::old('kategori'), array('id' => 'category', 'class' => 'form-control', )) }} 

上面的代碼基本上產生是這樣的:

<select id="category" class="form-control"> 
    <option value="">Select a Kategori</option> 
    <option value="1">First Kategori</option> 
    <option value="2">Second Kategori</option> 
</select> 

Form::select()中的第三個參數需要一個默認選項值,如果您提供一個值,它將與選項值進行比較,並顯示匹配的選項值。

和你的窗體的方法不應該是'method' => 'POST''method' => 'put'

0

我使用這等你描述的情況下面的代碼。在我的控制,我有

public function create() 
    { 
     $category1= Category1::lists('category1','id'); 
     $category2= Category2::lists('category2','id'); 
     //you can have many select lists in an form 
     return  
    View::make('someobject.create',compact('category1','category2')); 
    } 

在我someobject.create視圖

{{Form::open(array('route'=>'someobject.store'))}} 
    <div> 
    {{Form::label('category1', 'category1')}} 
    {{Form::select('category1',array('' => 'Please select') + $category1) }} 
    <span class="errors">{{$errors->first('category1')}}</span> 
    {{Form::label('category2', 'category2')}} 
    {{Form::select('category2',array('' => 'Please select') + $category2) }} 
    <span class="errors">{{$errors->first('category2')}}</span> 
    </div> 
    <div> 
    {{Form::submit('Save',array('class' => 'btn'))}} 
    </div>