我正在尝试升级我的搜索工具,目前我列出了所有可用的技能,一旦点击它就会搜索具有该技能的handymens。现在我想创建一个搜索框,这样一旦用户输入任何内容,一旦点击按钮,它将搜索并显示所有带有该技能的handymens。所以例如'p'会返回'Plumber'。但我正在努力解决它所以请帮助我,如果需要附加任何其他文件或数据库请告诉我。
所以基本上它应该是这样的,用户输入一个角色。信息将传递到技能数据库,并查找与输入的任何字符匹配的技能。然后,因为技能和杂工表已连接,所以输出应该是具有该技能的杂工列表,并且它们在联结表中定义。单击其中一个链接后,应显示特定勤杂工的详细信息。理想情况下,一切都应该在1页上。我一直在玩,但仍然无法解决它。
这就是我得到的atm:RouteCollection.php第161行中的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
控制器:
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');
}
}
Route.php:
Route :: group(['middleware'=&gt; ['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');
});
答案 0 :(得分:1)
我会做这样的事情:
Handyman
模型上的有一个名为skills
的方法:
public function skills() {
return $this->belongsToMany('App\Skills');
}
并在Skills
模型上有一个名为handymen
public function handymen() {
return $this->belongsToMany('App\Handymen');
}
这样我们就可以在模型上建立多对多的关系
与details
上的Controller
方法相比,您可以执行以下操作:
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');