Przemek Wojtas
Przemek Wojtas

Reputation: 1381

Laravel displaying data from 2 tables

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?

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

Upvotes: 1

Views: 802

Answers (1)

Laerte
Laerte

Reputation: 7083

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:

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:

@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:

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...

Upvotes: 1

Related Questions