2016-11-27 14 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というエラーが出ます:。。*選択をcompaniesservicescompany_id = companiesidnameからLIKE%xx%およびcompaniesdeleted_atはnullです)

この検索はどのように行う必要がありますか?

ありがとうございます!

更新:句が問題を引き起こしている場合に

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

はcompanies'テーブル ''であなたcompany_id'コラムをお持ちですか? – 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

喜んで助けてください... –