2017-02-09 7 views
1

一連のリンク先ページを検索して結果を返す必要があります。私は何かを検索した場合、それは私が検索したものを返し、私は状況によってフィルタリングする場合には、ステータス罰金であることを返し文字列とステータスを使用して雄弁を使って検索する

/** 
* @var \Illuminate\Database\Eloquent\Builder 
*/ 
private $query; 

/** 
* @param string $status 
* @param string $search 
* @param int $offset 
* @return \Illuminate\Database\Eloquent\Collection 
*/ 
public function getLandingPageListing($status = 'all', $search = '', $offset = 0) 
{ 
    $this->query = $this->model->newQuery(); 
    $this->setRelationship(); 
    $this->setStatus($status); 
    $this->setSearch($search); 
    return $this->getLandingPages(); 
} 

private function setRelationship() 
{ 
    $this->query->with('message'); 
} 

/** 
* @param string $status 
*/ 
private function setStatus($status) 
{ 
    if ($status !== 'all') { 
     $this->query->where('lp_status', $status); 
    } 
} 

/** 
* @param string $search 
*/ 
private function setSearch($search) 
{ 
    if ($search !== '') { 
     $this->query->where('lp_title', 'LIKE', '%' . $search . '%'); 
     $this->query->orWhere('lp_description', 'LIKE', '%' . $search . '%'); 
     $this->query->orWhere('lp_domain', 'LIKE', '%' . $search . '%'); 
     $this->query->orWhereHas('message', function ($query) use ($search) { 
      $query->where('message_subject', 'LIKE', '%' . $search . '%'); 
     }); 
    } 
} 

/** 
* @return \Illuminate\Database\Eloquent\Collection 
*/ 
private function getLandingPages() 
{ 
    return $this->query->get(); 
} 

:現在、私はこのコードを持っています。しかし、フィルターをかけて検索すると、それは機能せず、最後のアクションだけが返されます。

だから、アクティブにフィルタリングすると、すべてのアクティブなランディングページが返されます。これらのアクティブなページで「Example」を検索すると、アクティブなものだけでなく、すべての「Examples」が返されます。

どうすればこの問題を解決できますか?

答えて

1

括弧が必要です。あなたのorWhereメソッドはあなたのロジックを破るので、結果としてexampleを含むすべてのページを返します。あなたはこのコードブロックのようなものでそれを行うことができます。

private function setSearch($search) 
{ 
    if ($search !== '') { 
     $this->query(function($query) use ($search){ 
      $query->where('lp_title', 'LIKE', '%' . $search . '%'); 
      $query->orWhere('lp_description', 'LIKE', '%' . $search . '%'); 
      $query->orWhere('lp_domain', 'LIKE', '%' . $search . '%'); 
      $query->orWhereHas('message', function ($query) use ($search) { 
       $query->where('message_subject', 'LIKE', '%' . $search . '%'); 
      }); 
     }); 
    } 
} 

あなたはそれについてhere詳細を学ぶことができます。

+0

これは機能します。しかし、フィルタを変更すると実際の検索が覚えられなくなりますが、もう少し詳しく調べていきます。助けてくれてありがとう。 :) – Albert

関連する問題