2017-04-22 11 views
0

私はlaravel backを使用しています。バンドルと学習という2つのテーブルがあります。私は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", 
      ]); 

だから、plsは私がログインしているユーザーが作成したドロップダウンリストでレコードだけではないすべてのレコードを追加するために、問題を解決するのに役立ちます。..ありがとう

+0

model_functionを使用するか、resources/views/vendor/backpack/crud/fieldsにカスタムフィールドを作成し、そこに条件を追加するか、スタディモデル(または必要なモデル)にグローバルスコープを追加しますソリューション) – Indra

答えて

0

私は最善の方法は、それを追加モデル、UserStudyを作成することだと思う:

  • は、研究を拡張し、

  • には、現在のユーザーが見ることのできるフィルタリングのグローバルスコープがあります。あなたは、代わりに、研究の、あなたのフィールド定義にこの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); 
          }); 
         } 
        } 
    } 
    

それは次のようになります。 App\Models\StudyApp\Models\UserStudyに置き換えてください。

希望します。乾杯!

+0

親愛なる親愛なる..それを試してみる。 –

関連する問題