2016-04-14 38 views
0

我正在尝试升级我的搜索工具,此刻我列出了所有可用的技能,并且一旦单击它,它就会搜索具有该技能的handymens。现在我想创建一个搜索框,以便一旦用户输入任何内容,一旦点击按钮,就会搜索并显示所有这些技能。例如'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你的搜索服务。
在我有几十万条记录的情况下,它确实很棒,而且它的设置非常简单。
另外,还有一个名为Elasticquent的库,使Elasticsearch更易于在Laravel中使用。

+0

我想让事情变得简单,因为那是我被要求做的事情,而不使用这种服务 –