2017-04-22 86 views
0

我正在使用laravel,而且我有兩個稱爲捆綁和研究的表格。我從bundleCrudController的表單中添加了一個下拉字段。但我只想在下拉列表中添加那些由登錄用戶創建的研究,而不是研究表中的所有數據。在laravel揹包中的下拉列表中添加自定義值

這裏是我的代碼中加一滴數據下拉列表 -

$this->crud->addField([ 
       'name' => 'studies', 
       'label' => 'Studies', 
       'type' => 'select2_from_array', 
       'options' => $this->Study->getUnallocatedStudies($entryId), 
       'allows_null' => false, 
       'hint' => 'Search for the studies you would like to add to this bundle', 
       'tab' => 'Info', 
       'allows_multiple' => true 
      ]); 

     $this->crud->addColumn([ 
       'label' => 'Studies', 
       'type' => "select_multiple", 
       'name' => 'bundle_id', 
       'entity' => 'studies', 
       'attribute' => 'name', 
       'model' => "App\Models\Study", 
      ]); 

所以請幫助我解決這個問題,以通過登錄的用戶不是所有記錄創建的下拉列表只添加這些記錄..感謝名單

+0

使用model_function或在resources/views/vendor/backpack/crud /字段中創建一個自定義字段,並在該模型或研究模型(或任何你需要的模型)中添加條件添加一個全局範圍(查看所有這些手冊解決方案) – Indra

回答

0

我認爲最好的方法是創建一個額外的模型,UserStudy,即:

  • 延伸研究;

  • 具有全局範圍,用於過濾當前用戶可以看到的內容;

它應該是這個樣子:

<?php 

namespace App\Models; 

use App\Models\Study; 
use Auth; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Database\Eloquent\Builder; 

class UserStudy extends Study 
{ 
    /** 
    * The "booting" method of the model. 
    * 
    * @return void 
    */ 
    protected static function boot() 
    { 
     parent::boot(); 

     // filter out the studies that don't belong to this user 
     if (Auth::check()) { 
      $user = Auth::user(); 

      static::addGlobalScope('user_id', function (Builder $builder) use ($user) { 
       $builder->where('user_id', $user->id); 
      }); 
     } 
    } 
} 

然後您就可以在你的領域定義中使用這個UserStudy模型,而不是研究。只需用App\Models\UserStudy替換App\Models\Study即可。

希望它有幫助。乾杯!

+0

Thanx親愛的..會試試看。 –

相關問題