2016-10-28 3 views
0

私は、関係オプションを使用するCRUDを生成したNewsとNewsCategoriesモデルを持っています。InfyOmジェネレータの関係

ニュースモデルの選択リストを生成して、それが属するNewsCategoryを選択する必要があります。

私はモデルでこれを行う方法を知っていますが、リポジトリパターンを使用してそれを行う方法はわかりません。

私はドキュメントの例を見ることができませんので、これについての助けに感謝します。

おかげ

NewsRepository

/** 
    * Configure the Model 
    **/ 
    public function model() 
    { 
     return News::class; 
    } 

ニュースモデル

/** 
    * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 
    **/ 
    public function newsCategory() 
    { 
     return $this->belongsTo(NewsCategory::class); 
    } 

ニュースコントローラー

/** 
    * Show the form for creating a new News. 
    * 
    * @return Response 
    */ 
    public function create() 
    { 
     return view('news.create'); 
    } 

    /** 
    * Store a newly created News in storage. 
    * 
    * @param CreateNewsRequest $request 
    * 
    * @return Response 
    */ 
    public function store(CreateNewsRequest $request) 
    { 
     $input = $request->all(); 

     $news = $this->newsRepository->create($input); 

     Flash::success('News saved successfully.'); 

     return redirect(route('news.index')); 
    } 

答えて

0

リポジトリはInfyOm\Generator\Common\BaseRepositoryを拡張する場合。リポジトリは、それ自身でモデル関係を更新する必要があります。関係の値を他の入力と一緒に正しいキーで渡すだけです。

ただし、削除と読み取り(アクションと呼ばせてください)には、データをクエリする必要があります。

これは、リポジトリメソッド、スコープクエリ、または基準クラスを使用して行うことができます。 (これらのフィルタを呼び出します)。

  1. リポジトリ方法:

    // inside your controller 
    // some repository filtering method 
    $this->repository->whereHas('newsGroup', function($query){...}); 
    $this->repository->hidden(['field_to_hide']); 
    ... 
    // some action: delete, all or findWhere... 
    $this->repository->delete(); 
    
  2. スコープクエリが雄弁モデルにいくつかのクエリを適用し、それを返すコールバックです(受け入れ、雄弁\ビルダー\データベースを返す雄弁スコープとは異なります)

    $this->repository->scopeQuery(
         function ($model){ return $model->where(...); 
    }); 
    Or your 
    // some action: delete, update or findWhere... 
    $this->repository->delete(); 
    
  3. クライテリアウェイ:クエリーを担当するクラスを作成します。シンプルなユースケースのためには過剰です。

    // inside the controller 
    $this->repository->pushCriteria(new NewsBelongingToCategory ($group_id)); 
    
    // App\Criteria\NewsBelongingToCategory.php 
    class NewsBelongingToCategory implements CriteriaInterface { 
    
        private $group_id; 
    
        public function __construct($group_id){ 
         $this->group_id = $group_id; 
        } 
    
        public function apply($model, NewsRepositoryInterface $repository) 
         { 
          $group_id = $this->group_id; 
          $model = $model->whereHas('newsCategory', 
            function ($query) use ($group_id){ 
             $query->where('group_id', '=', $group_id); 
            }); 
           return $model; 
         } 
    } 
    
    // in your controller 
    
    $this->repository->delete(); 
    

    一部のアクションは特定のフィルタを無視します。たとえば、delete(id)update($attributes, $id)は条件を使用しませんが、lists($column, $key)はスコープを使用しません。