Laravel 搜索查询
Laravel search query
我正在尝试执行搜索查询,搜索服务标题、描述和公司名称(公司有服务,如果公司名称匹配,它 returns 服务)。
我有一个搜索字段,它被传递到我的控制器。
我试过这样:
$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);
但是我得到一个错误,Unknown column 'services.company_id' in 'where clause' (SQL: select * from companies
where services
. company_id
= companies
.id
和 name
LIKE %xx% 并且 companies
.deleted_at
为空)
我该如何搜索?
谢谢!
更新:
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();
});
where 子句中的 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);
我正在尝试执行搜索查询,搜索服务标题、描述和公司名称(公司有服务,如果公司名称匹配,它 returns 服务)。
我有一个搜索字段,它被传递到我的控制器。 我试过这样:
$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);
但是我得到一个错误,Unknown column 'services.company_id' in 'where clause' (SQL: select * from companies
where services
. company_id
= companies
.id
和 name
LIKE %xx% 并且 companies
.deleted_at
为空)
我该如何搜索?
谢谢!
更新:
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();
});
where 子句中的 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);