<?php
/**
 * User: Mike
 * Email: m@9026.com
 * Date: 2017/1/12
 * Time: 17:52
 */

namespace App\Repositories\System\Criteria;




use App\Repositories\Base\Criteria;
use App\Repositories\Contracts\RepositoryInterface as Repository;

class MultiWhere extends Criteria {

    private $search = [];

    /**
     * MultiWhere constructor.
     * @param array $search
     *
     */
    public function __construct(array $search)
    {
        $this->search = $search;
    }

    /**
    * @param $model
    * @param RepositoryInterface $repository
    * @return mixed
    */
    public function apply($model, Repository $repository)
    {
        $keyword = $this->search['keyword'];
          if(isset($keyword) && $keyword) {
                $model = $model->where('message','like','%'.$keyword.'%')
                ->orWhere('id',$keyword)
                ->orWhereHas('user',function ($query) use ($keyword){
                    $query->where('nickname','like','%'.$keyword.'%');
                });
          }
         return $model;
    }

}