gpt4 book ai didi

php - 在 Laravel 中创建搜索工具

转载 作者:行者123 更新时间:2023-11-29 21:08:16 29 4
gpt4 key购买 nike

我正在尝试升级我的搜索工具,目前我列出了所有可用的技能,一旦单击它就会搜索具有该技能的杂工。现在我想创建一个搜索框,这样一旦用户输入任何内容,一旦单击按钮,它将搜索并显示具有该技能的所有杂工。因此,例如“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

Controller :

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');
}
}

最佳答案

这可能无法直接回答您的问题,但我建议您使用 ElasticSearch 之类的搜索服务。
对于我有几十万个条目的情况来说,它非常有效,而且设置起来非常容易。
另外,还有一个名为 Elasticquent 的库,可以使 Elasticsearch 在 Laravel 中更易于使用。

关于php - 在 Laravel 中创建搜索工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36620604/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com