2016-11-27 4 views
1

Я пытаюсь выполнить поисковый запрос, который выполняет поиск в названиях сервисов, описаниях и названиях компаний (компания имеет службы, если название компании соответствует, оно возвращает службы).Поисковый запрос Laravel

У меня есть поле поиска, которое передается моему контроллеру. Я пробовал так:

$query = Service::select('id','company_id','title','description',price); 
$search = $request->input('search',null); 
$query = is_null($search) ? $query : $query->where('title','LIKE','%'.$search.'%')->orWhere('description','LIKE','%'.$search.'%')->orWhereHas('company', function ($q) use ($search) 
    { 
     $q->where('name','LIKE','%'.$search.'%')->get(); 
    }); 


$services= $query->paginate(5); 

Но я получаю сообщение об ошибке Неизвестный столбец 'services.company_id' в 'где предложение' (SQL: SELECT * FROM companies где servicescompany_id = companiesid и name.. LIKE% xx% и companies. deleted_at is null)

Как я должен выполнить поиск?

Спасибо!

Update:

class Service extends Model 
{ 
use SoftDeletes; 

protected $dates = ['deleted_at']; 

public function company() { 

    return $this->belongsTo('Company'); 

} 
} 

class Company extends Model 
{ 
    use SoftDeletes; 

    protected $dates = ['deleted_at']; 

    public function services() { 
    return $this->hasMany('Service'); 
} 
} 

Schema::create('services', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('company_id'); 
     $table->integer('service_category_id'); 
     $table->integer('server_id'); 
     $table->string('title'); 
     $table->string('description'); 
     $table->string('icon'); 
     $table->boolean('accepts_swaps'); 
     $table->integer('qty_available'); 
     $table->double('price_usd', 10, 6); 
     $table->timestamps(); 
     $table->softDeletes(); 
    }); 

Schema::create('companies', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('owner_id'); 
     $table->string('name'); 
     $table->string('email'); 
     $table->string('paypal_email')->nullable(); 
     $table->string('skrill_email')->nullable(); 
     $table->string('contact_email')->nullable(); 
     $table->string('phone')->nullable(); 
     $table->integer('city_id')->nullable(); 
     $table->string('short_description')->nullable(); 
     $table->text('description')->nullable(); 
     $table->integer('subscription_id')->nullable(); 
     $table->timestamp('subscription_end_date')->nullable(); 
     $table->string('avatar')->default("img/default/user-avatar-128.min.png"); 
     $table->integer('highlighted_game_id')->nullable()->default(null); 
     $table->timestamps(); 
     $table->softDeletes(); 
    }); 
+0

У вас 'company_id' столбец' companies' таблицы? – piotr

+0

Можете ли вы показать свою схему таблиц и отношения в своем вопросе? –

+0

обновлен, проверьте его. благодаря! – user3844579

ответ

2

get функция где положение вызывает проблему. Попробуйте удалить get.

Так что ваш код будет выглядеть так:

$query = Service::select('id','company_id','title','description',price); 
$search = $request->input('search',null); 
$query = is_null($search) ? $query : $query->where('title','LIKE','%'.$search.'%')->orWhere('description','LIKE','%'.$search.'%')->orWhereHas('company', function ($q) use ($search) 
    { 
     $q->where('name','LIKE','%'.$search.'%'); 
    }); 


$services= $query->paginate(5); 
+0

спасибо, сейчас работает! – user3844579

+0

Рад помочь ... –

Смежные вопросы