Muaath Alhaddad
Muaath Alhaddad

Reputation: 361

How to display a column of different tables using the pivot table?

I am beginner to Laravel and now I am doing this project but I am stuck in this point for a couple days. Here is what I wish do, I would like to display the day and timeslot labels selected by the tutor!

here is my tables and their relationships https://i.sstatic.net/11o8a.jpg

Day Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Day extends Model
{
    protected $guarded =  ['id', '_token']; 
}

Timeslot Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Timeslot extends Model
{
    protected $guarded = ['id', '_token'];
    
    public function Tutor()
    {
        return $this->hasMany(Tutor::class);
    }
}

Tutor_availability Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tutor_availability extends Model
{
    protected $fillable = ['tutor_id', 'timeslot_id', 'day_id'];
    protected $guarded =  ['id', '_token'];
    
    public function tutor()
     {
        return $this->belongsTo('App\Tutor');
     }
    public function day()
     {
        return $this->belongsTo('App\Day');
     }
    public function timeslot()
     {
        return $this->belongsTo('App\Timeslot');
     }
}

tutor_availabilities Migration

    public function up()
    {
        Schema::create('tutor_availabilities', function (Blueprint $table) {
            // $table->bigIncrements('id');
            $table->integer('day_id')->unsigned();
            $table->foreign('day_id')->references('id')->on('days')->onDelete('cascade');
            $table->integer('timeslot_id')->unsigned();
            $table->foreign('timeslot_id')->references('id')->on('timeslots')->onDelete('cascade');
            $table->integer('tutor_id')->unsigned();
            $table->foreign('tutor_id')->references('id')->on('tutors')->onDelete('cascade');
            $table->timestamps();
            $table->primary(['day_id', 'timeslot_id', 'tutor_id']);
        });
    }

TutorController

  public function index()
    {
        $tutors = Tutor::latest()->paginate(5);



        $tutor_availabilities = Tutor_availability::all();
        

        return view('tutors.index', [
            'tutors' => $tutors, 
            'tutor_availabilities' => $tutor_availabilities    
        ]);
    }

View

                   <tr>
               <th>Days</th> <th> Timings</th>
           </tr>
           <tr>
                        <td>
                            {{$tutor_availabilities->day->label}}
                        </td> 
                        <td>
                            {{$tutor_availabilities->timeslot->label}}
                        </td>
             </tr>

Upvotes: 1

Views: 56

Answers (1)

Tanvir Ahmed
Tanvir Ahmed

Reputation: 1019

Put this in your Tutor_availability model

     public function day()
         {
            return $this->belongsTo(Day::class);
         }
    public function timeslot()
         {
            return $this->belongsTo(Timeslot:class);
         }

Then in controller

    public function index()
        {
            $tutor_availabilities = Tutor_availability::all();
            return view('tutors.index',
     compact('tutor_availabilities')) ;
        }

Then in view

    <table>
    @foreach ($tutor_availabilities as $tutor_available) 
    <tr><td>
                            {{$tutor_available->day->label}}
                        </td> 
                        <td>
                            {{$tutor_available->timeslot->label}}
                        </td></tr>
    @endforeach
    </table>

Upvotes: 3

Related Questions