2016-08-30 5 views
0

の柔軟性を得るためにどのように私は私に時間がかかるコーディングする文字列を追加しようとすると、PHPコードはlaravelのSQLクエリ

$datas = $this->model->ADD HERE->orderBy('created_at', 'DESC')->paginate(15); 

にSQLフィルタwhere('comment_id', '=', 1)を追加したいです。それを作る方法?文字列パラメータとしてSQLフィルタを渡す

CommentResource.php:


は、ここに私のコードです。

<?php 
class CommentResource extends BaseResource 
{  
    public function index() 
    { 
     $filter = "where('comment_id', '=', 1)"; 
     return parent::index_filter($filter); 
    } 

CommentResource.php

<?php 
class BaseResource extends Controller 
{ 
    protected function index_filter($filter) 
    { 
     $datas = $this->model->ADD HERE->orderBy('created_at', 'DESC')->paginate(15); 
     return view($this->resourceView.'.index')->with('datas', $datas); 
    } 
} 

答えて

0

私はあなたのクエリでフィルタとしてさまざまなタイプのwhereを使いたいと思っています。そのため、それらを動的にしたいのです。私はあなたの仕事のために、以下のソリューションをお勧めします:

<?php 
class CommentResource extends BaseResource 
{  
    public function index() 
    { 
     $filter = [ 'operator' => 'where', 'args' => ['comment_id', '=', 1]]; 
     return parent::index_filter($filter); 
    } 

<?php 
class BaseResource extends Controller 
{ 
    protected function index_filter($filter) 
    { 
     $where = $filter['operator']; 
     $args = $filter['args']; 
     $datas = $this->model->$where(...$args)->orderBy('created_at', 'DESC')->paginate(15); 
     return view($this->resourceView.'.index')->with('datas', $datas); 
    } 
} 

しかし、それは理由oeprator ...

+0

それは動作し、それは私が欲しいものです。ありがとうございました。タイトルをより正確に変更することを検討します。あなたのアドバイスがありますか? –

+0

数日後に、@Andrejの回答を改善しました。要するに、$ filterは配列を入れ子にするために成長し、 '... $ args'は$ argsに変更されます。私はこれらのコードを私の答えに掲示し、私が '... $ args'を変更する理由を説明しようとします。 –

0

私は私が正しくあなたの要件を得たかどうかわからないですが、あなたは別にfieldvalueを受け入れるようにindex_filter書き換える場合は、あなたが可能なユーザlaravelから定期where()

protected function index_filter($field,$value) 
    { 
    $datas = $this->model->where($field,$value)->orderBy('created_at', 'DESC')->paginate(15); 
    return view($this->resourceView.'.index')->with('datas', $datas); 
} 

ドキュメントhereがあります。場合あなたは本当により多くの柔軟性が必要です。

protected function index_filter($filter) 
    { 
    $datas = $this->model->whereRaw($filter)->orderBy('created_at', 'DESC')->paginate(15); 
    return view($this->resourceView.'.index')->with('datas', $datas); 
} 

を、これはあなたが悪意のあるコードを注入する可能性を公開するように、それは間違いなく適切に事前にエスケープする必要があります、本当に危険であることにかかわらず、心を持っています。

+0

ありがとうございます。しかし、私はちょうどどこよりも柔軟性が必要です。 –

+0

ok、私は答えを改善しました:) – MatFiz

+0

'whereRaw'は私の質問を解決する方法です。 詳しくは、 '$ filter = 'comment_id = 1';'を 'index_filter($ filter)'に渡してください。次に、このメソッドでは、 '$ datas = $ this-> model-> whereRaw($ filter) - > orderBy( '​​created_at'、 'DESC') - > paginate(15);'のように動作します。 私は注射予防に関する経験がないので、しばらくの間勉強します。それからあなたと話し合うことができます。 –

0

私の最新のコードは、右の作品のPhp5.6 +から始めていきます。私はここに投稿します。

<?php 
class CommentResource extends BaseResource 
{  
    public function index() 
    { 
     $options = [ 
      'filters'=>[ 
         [ 'operator' => 'where', 
         'args'  => [ 
             [ 'article_id', '=', $article_id ], 
             [ 'comment_id', '=', $comment_id ], 
             // add filter args... 
             ], 
         ], 
         // add filter operators here... 
        ], 
      'sorts' => [ 
         'column'  => $sortColumn, // change sort column... 
         'order'  => $sortOrder, // change sort order... 
        ], 
        ]; 
     return parent::index_filter($options); 
    } 

<?php 
class BaseResource extends Controller 
{ 
    protected function index_filter($options, $number=15) 
    { 
     $result = $this->model; 
     foreach ($options['filters'] as $filter) { 
      $operator = $filter['operator']; 
      $args  = $filter['args']; 
      $result = $result->$operator($args); 
     } 
     if ($options['sorts'] != []) { 
      $column = $options['sorts']['column']; 
      $order = $options['sorts']['order']; 
      $result = $result->orderBy($column, $order); 
     } 
     return $result->paginate($number); 
    } 
} 

私は$ argsをに... $ argsをを変更する理由は、 '引数が' の値よりも多くを持っているとき、例えば、

    'args'  => [ 
           [ 'article_id', '=', $article_id ], 
           [ 'comment_id', '=', $comment_id ], 
           // add filter args... 
           ], 

... $ argsが「引数が変更されますされます'を1つの配列に渡しますが、$ argsはnest配列として' args 'のままになります。

関連する問題