gpt4 book ai didi

php - Laravel 搜索工具

转载 作者:行者123 更新时间:2023-11-28 23:33:21 25 4
gpt4 key购买 nike

我正在尝试升级我的搜索工具,目前我正在列出所有可用的技能,一旦点击它就会搜索具有该技能的杂工。现在我想创建一个搜索框,这样一旦用户输入任何内容,它就会搜索并在单击按钮后显示所有具有该技能的杂工。因此,例如“p”将返回“管道工”。但我正在为此苦苦挣扎,所以请帮助我,如果需要附加任何其他文件或数据库,请告诉我。

所以基本上它应该是这样的,用户输入一个字符。信息被传递到技能数据库并找到与输入的任何字符匹配的技能。然后因为技能和杂工表是连接的,所以输出应该是具有该技能的杂工列表,并且它们在联结表中定义。单击其中一个链接后,应显示特定勤杂工的详细信息。理想情况下,所有内容都应在一页上。我一直在玩,但仍然无法解决。

这是我在 RouteCollection.php 第 161 行中得到的 atm: NotFoundHttpException:

查看:

    <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()
{
$handymen = $skill->handymen;
$q = Input::get('skill');
if($q && $q != ''){
$searchTerms = explode(' ', $q);
$query = DB::table('skills'); // it's DB::table(), not DB::tables

if(!empty($searchTerms)){

foreach($searchTerms as $skill) {
$query->where('skill', 'LIKE', '%'. $skill .'%');
}
}
$results = $query->get();

dd($results); // for debugging purpose. Use a View here

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

路线.php:

Route::group(['middleware' => ['web']], function () {

Route::get('home', 'HandymanController@home');

Route::get('search', 'HandymanController@search');

Route::post('searchTesting', 'HandymanController@searchTesting');

Route::post('details}', 'HandymanController@details');

Route::get('skilledHandyman/{handymanId}', 'HandymanController@skilledHandyman');

Route::post('jobassign', 'HandymanController@jobassign');

//Route::get('assignjob/{handymanId}', 'HandymanController@assignJob');

Route::get('addjob', 'HandymanController@addJob');

Route::post('addjform', 'HandymanController@addjForm');

Route::get('jobs', 'HandymanController@jobs');

Route::get('jobsdetails/{jobId}', 'HandymanController@jobsdetails');

Route::get('deletejob', 'HandymanController@deleteJob');

Route::post('deletejform', 'HandymanController@deletejForm');

});

最佳答案

我会做这样的事情:

  1. Handyman 模型上有一个名为 skills 的方法:

    public function skills() {
    return $this->belongsToMany('App\Skills');
    }
  2. Skills模型上有一个叫做handymen的方法

    public function handymen() {
    return $this->belongsToMany('App\Handymen');
    }

这样我们就可以在我们的模型上建立多对多的关系

  1. 对于 Controller 上的 details 方法,您可以这样做:

    function details() {
    $handymen = App\Handymen::whereHas('skills', function($query) {
    if(!empty($searchTerms)){
    foreach($searchTerms as $skill) {
    $query->where('skill', 'LIKE', '%'. $skill .'%');
    }
    }
    })->get();

    //dd($handymen);
    return view('search_results', compact('handymen'));
    }

我还没有测试过这个,但应该可以。

在你的 Routes.php 文件中确保你有:

Route::post('details', 'YourController@details');

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

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