我正在尝试升级我的搜索工具,目前我列出了所有可用的技能,一旦点击它就会搜索具有该技能的handymens。现在我想创建一个搜索框,这样一旦用户输入任何内容,一旦点击按钮,它将搜索并显示所有带有该技能的handymens。所以例如'p'会返回'Plumber'。但我正在努力解决它所以请帮助我,如果需要附加任何其他文件或数据库请告诉我。
查看:
<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 :(得分:0)
这可能不是您问题的直接答案,但我建议您使用ElasticSearch等搜索服务。
在我有几十万个条目的情况下,它的效果非常好,而且设置起来非常简单
另外,还有一个名为Elasticquent的库,可以让Lalastic中的Elasticsearch更容易使用。