Let me explain situation first.
让我先解释一下情况。
I am on the page with list of skills available such as "Plumber","Carpenter" and "Painter", when I click on one of those skills I want to get a list of handymans that have that skill and once clicked on a handyman I get full details about him.
我在页面上列出了可用的技能,例如“水管工”,“木匠”和“画家”,当我点击其中一项技能时,我想得到一份具有该技能的杂工列表,并且一旦点击了一个勤杂工我得到了关于他的全部细节。
Skills are displayed however when I click on one of the skills it doesn't want to retrieve any data. Both tables "handymen" and "skills" have many to many relationship and also there is a junction table. What am I doing wrong here?
但是当我点击其中一项不想要检索任何数据的技能时,会显示技能。两个表“handymen”和“skill”都有很多关系,并且还有一个联结表。我在这做错了什么?
Route::group(['middleware' => ['web']], function () {
Route::get('home', 'HandymanController@home');
Route::get('search', 'HandymanController@search');
Route::get('details/{handyman}', 'HandymanController@details');
Route::post('assignjob', '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');
Add Job View:
添加工作视图:
@extends('layouts.master')
@section('title', 'Add Job')
@section('header2')
<ul>
<li><a href="{{url('assignjob')}}">Assign job</a></li>
</ul>
@show
@section('content')
<h1>Handyman details</h1>
<ul>
@foreach ($handymen as $handyman)
<a href= "{{ url("HandymanController@details", $handyman->id) }}">
{{$handyman->name}}
</a>
@endforeach
</ul>
Controller:
function search()
{
$skills = Skill::all();
return view('layouts/search',['skills' => $skills]);
}
function details($skillId)
{
$skill = Skill::find($skillId);
$handymen = $skill->handymen;
return view('layouts/details', ['handymen' => $handymen]);
}
1 个解决方案
#1
1
According to your edited question, first you list all the skills in your search view. So, in your search view, you would have something like this:
根据您编辑的问题,首先列出搜索视图中的所有技能。所以,在你的搜索视图中,你会有这样的东西:
@foreach ($skills as $skill)
<a href= "{{ action("HandymanController@addjForm", $skill->id) }}">
{{ $skill->name }}
</a>
@endforeach
So you have to define in your routes file this route:
所以你必须在你的路线文件中定义这条路线:
Route::post('addjform/{skill}', 'HandymanController@addjForm');
This will point to Handyman Controller, where you will list all handymen that have that skill:
这将指向Handyman Controller,您将列出具有该技能的所有杂工:
public function addjForm(Request $request, Skill $skill)
{
$handymen = $skill->handymen;
return view('layouts/skilledHandymen', ['skill' => $skill,'handymen' => $handymen]);
}
For this to work, you have to define in Skill Model, the association:
为此,您必须在技能模型中定义关联:
public function handymen()
{
return $this->belongsToMany(Handyman::class,
'handyman_skill',
'skill_id',
'handyman_id');
}
The controller will point to a view where you will list all handymen that have such skill:
控制器将指向一个视图,您将列出具有此类技能的所有杂工:
In your case, it would be easier if you define an association in Skill model that links to Handyman:
在您的情况下,如果您在Skill模型中定义链接到Handyman的关联会更容易:
@foreach ($handymen as $handyman)
<a href= "{{ action("HandymanController@details", $handyman->id) }}">
{{ $handyman->name }}
</a>
@endforeach
When you choose a handyman, it will take you to Handyman controller details:
当你选择一个勤杂工时,它会带你到Handyman控制器的细节:
function details(Request $request, Handyman $handyman)
{
return view('layouts/details', ['handymen' => $handymen]);
}
For this you will define this route:
为此,您将定义此路线:
Route::get('/handyman/{handyman}', 'Handyman@details');
And this will point you finally to the details of chosen handyman, and you can show his details:
这将最终指向你所选择的勤杂工的细节,你可以展示他的细节:
<p>{{ $handyman->id }}<p>
<p>{{ $handyman->name }}</p>
The thing that is important to understand here is that you will first have a Collection of skills that will lead you to a Collection of Handymen, and not just a single one. After you choose a Handyman from the second list you will be able to show his details. If you try to jump over this step you will be trying to show details of a list.
在这里要理解的重要事情是,您将首先获得一系列技能,这些技能将引导您进入杂工收藏,而不仅仅是一个。从第二个列表中选择一个杂工后,您将能够显示他的详细信息。如果您尝试跳过此步骤,则会尝试显示列表的详细信息。
Hope this helps...
希望这可以帮助...
#1
1
According to your edited question, first you list all the skills in your search view. So, in your search view, you would have something like this:
根据您编辑的问题,首先列出搜索视图中的所有技能。所以,在你的搜索视图中,你会有这样的东西:
@foreach ($skills as $skill)
<a href= "{{ action("HandymanController@addjForm", $skill->id) }}">
{{ $skill->name }}
</a>
@endforeach
So you have to define in your routes file this route:
所以你必须在你的路线文件中定义这条路线:
Route::post('addjform/{skill}', 'HandymanController@addjForm');
This will point to Handyman Controller, where you will list all handymen that have that skill:
这将指向Handyman Controller,您将列出具有该技能的所有杂工:
public function addjForm(Request $request, Skill $skill)
{
$handymen = $skill->handymen;
return view('layouts/skilledHandymen', ['skill' => $skill,'handymen' => $handymen]);
}
For this to work, you have to define in Skill Model, the association:
为此,您必须在技能模型中定义关联:
public function handymen()
{
return $this->belongsToMany(Handyman::class,
'handyman_skill',
'skill_id',
'handyman_id');
}
The controller will point to a view where you will list all handymen that have such skill:
控制器将指向一个视图,您将列出具有此类技能的所有杂工:
In your case, it would be easier if you define an association in Skill model that links to Handyman:
在您的情况下,如果您在Skill模型中定义链接到Handyman的关联会更容易:
@foreach ($handymen as $handyman)
<a href= "{{ action("HandymanController@details", $handyman->id) }}">
{{ $handyman->name }}
</a>
@endforeach
When you choose a handyman, it will take you to Handyman controller details:
当你选择一个勤杂工时,它会带你到Handyman控制器的细节:
function details(Request $request, Handyman $handyman)
{
return view('layouts/details', ['handymen' => $handymen]);
}
For this you will define this route:
为此,您将定义此路线:
Route::get('/handyman/{handyman}', 'Handyman@details');
And this will point you finally to the details of chosen handyman, and you can show his details:
这将最终指向你所选择的勤杂工的细节,你可以展示他的细节:
<p>{{ $handyman->id }}<p>
<p>{{ $handyman->name }}</p>
The thing that is important to understand here is that you will first have a Collection of skills that will lead you to a Collection of Handymen, and not just a single one. After you choose a Handyman from the second list you will be able to show his details. If you try to jump over this step you will be trying to show details of a list.
在这里要理解的重要事情是,您将首先获得一系列技能,这些技能将引导您进入杂工收藏,而不仅仅是一个。从第二个列表中选择一个杂工后,您将能够显示他的详细信息。如果您尝试跳过此步骤,则会尝试显示列表的详细信息。
Hope this helps...
希望这可以帮助...