2016-04-14 18 views
0

検索機能をアップグレードしようとしていますが、利用可能なすべてのスキルが一覧表示されていて、そのスキルを使用してハンディメントを検索すると、今度は、検索ボックスを作成して、ユーザーが何かを入力すると、ボタンをクリックすると、そのスキルを持つすべてのハンディーメンを検索して表示します。例えば ​​'p'は '配管'を返します。しかし、私はそれで苦労しているので、他のファイルやデータベースを添付する必要がある場合は私に教えてください。laravelで検索機能を作成する

ビュー:

<h1>Here you can search</h1> 
    <form action="{{url('details')}}" method="POST"> 
    {{ csrf_field() }} 
     <div> 
      <input type='text' name='skill'/> 
     </div> 
    <input type="submit" name="submitBtn" value="Search"> 
    </form> 
    @foreach ($skills as $skill) 
     <p> 
      <a href="{{url('details/'.$skill->id)}}">{{$skill->skill}}</a>   
     </p> 

    @endforeach 
@endsection 

コントローラー:

function search() 
{ 
    $skills = Skill::all(); 
    return view('layouts/search',['skills' => $skills]); 
} 
function details($skillId) 
{ 
$skill = Skill::find($skillId); 
$handymen = $skill->handymen; 
$skill = Input::get('skill'); 
$result = Handyman::where('skills','LIKE','%'.$skill.'%') 
      ->orWhere('email','LIKE','%'.$skill.'%') 
      ->get(); 
return view('layouts/details', ['skill' => $skill,'handymen' => $handymen]); 
} 

便利屋データベース:

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateHandymenTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
public function up() 
{ 
    Schema::create('handymen', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('first_name'); 
     $table->string('last_name'); 
     $table->string('street'); 
     $table->string('postcode'); 
     $table->string('town'); 
     $table->string('skills'); 


     $table->integer('job_id')->unsigned(); 
     $table->foreign('job_id')->references('id')->on('jobs')->onDelete('cascade'); 


     $table->timestamps(); 
     }); 
    } 
/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::table('handymen', function (Blueprint $table) { 
     $table->dropForeign('handymen_job_id_foreign'); 
     $table->dropColumn('job_id'); 
    }); 
    } 
} 

スキル表:

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateSkillsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('skills', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('skill'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('skills'); 
    } 
} 

ジャンクションテーブル:

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateHandymanSkillTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('handyman_skill', function (Blueprint $table) { 
      $table->integer('handyman_id')->unsigned(); 
      $table->integer('skill_id')->unsigned(); 
      $table->timestamps(); 
     }); 
     Schema::table('handyman_skill', function ($table) { 
      $table->primary(['handyman_id', 'skill_id']); 
      $table->foreign('handyman_id')->references('id')->on('handymen')->onDelete('cascade'); 
      $table->foreign('skill_id')->references('id')->on('skills')->onDelete('cascade'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('handyman_skill'); 
    } 
} 

答えて

0

これは、あなたの質問に直接答えではないかもしれないが、私はあなたの検索サービスのためElasticSearchのようなものを使用してお勧めします。
私のケースでは数十万のエントリがあり、セットアップがとても簡単です。
さらに、LaravelでElasticsearchを使いやすくするために、Elasticquentというライブラリがあります。

+0

私は、そのようなサービスを使用せずに、私がやったことであるように物事を単純にしています –