简体   繁体   中英

Laravel Eloquent ORM Relationships two tables displaying one column

I think I'm misunderstanding relationships, I have two tables one called invites and one called teams

Invites columns:

  • id
  • teams_id
  • sentUserID
  • userInvID

Teams columns:

  • id
  • name
  • ownerID

What I'm trying to do is, display in a HTML table the name column from the teams table using the teamid column from the invites table to display each invite with the team name, so far I have set up a hasMany relationship so the teams table hasMany invites, and I have tried my best to set this functionality up but I think I'm misunderstanding Eloquent ORM Relationships, help please, below is what I have so far

My model, teams.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Teams extends Model
{
   public function invite()
   {
      return $this->hasmany('App\Invite');

   }

}

My Teams Controller TeamsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Teams;
use Illuminate\Support\Facades\DB;
use Session;
use Auth;
use App\Invite;

class TeamsController extends Controller
{
    public function invites()
    {
        $id = Auth::id();
        $invites = Invite::where('sentUserID', '=', $id)->get();
        $teams = Teams::with('invite')->pluck('name');
        return view('team.invites', ['invites' => $invites, 'teams' => $teams]);

    }
}

My view invites.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">View Teams & Manage</div>

                <div class="panel-body">
                        <table class="table table-hover table-dark">
                          <thead>
                            <tr>
                              <th scope="col">Team Name</th>
                              <th scope="col">Actions</th>
                                <th scope="col">Actions</th>
                            </tr>
                          </thead>
                          <tbody>
                            @foreach($invites as $invite)
<tr>
                              <th scope="row">{{$teams}}</th>
                              <td><a class="btn btn-success" href="#">Accept</a></td>
                                <td><a class="btn btn-danger" href="#">Decline</a></td>
                            </tr>

@endforeach                       </tbody>
                        </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

All I can seem to do with this code is display a list of names for each invite.

Since you are using pluck('name') you will get only the column values for the column name

You can try to restrict the columns via the select() . You can specify name column in the select clause, id column is required because it links the teams and invites table/records

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Teams;
use Illuminate\Support\Facades\DB;
use Session;
use Auth;
use App\Invite;

class TeamsController extends Controller
{
    public function invites()
    {
        $id = Auth::id();
        $invites = Invite::where('sentUserID', '=', $id)->get();
        $teams = Teams::with('invite')->select('id', 'name');
        return view('team.invites', ['invites' => $invites, 'teams' => $teams]);

    }
}

However, since you are iterating over the $invites in the blade and need only the name value from team column you can use eager loading and load the team record with the invite query

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Teams;
use Illuminate\Support\Facades\DB;
use Session;
use Auth;
use App\Invite;

class TeamsController extends Controller
{
    public function invites()
    {
        $id = Auth::id();
        $invites = Invite::where('sentUserID', '=', $id)
            ->with('team:id,name')
            ->get();
        
        return view('team.invites', ['invites' => $invites]);
    }
}

And in the blade view

//...
@foreach($invites as $invite)
    <th scope="row">{{ $invite->team->name }}</th>
    <td>
        <a class="btn btn-success" href="#">Accept</a>
    </td>
    <td>
        <a class="btn btn-danger" href="#">Decline</a>
    </td>
@endforeach
//...

The relations as apparent from your table structure can be defined as follows

class Team extends Model
{
    public function invites()
    {
        return $this->hasMany(Invite::class);
    }
}


class Invite extends Model
{
    public function team()
    {
        return $this->belongsTo(Team::class);
    }
}

As per the table structure in your question the relation can be either

  • Team hasOne Invite OR Team hasMany Invite(s)
  • Invite belongsTo Team

If an Invite can have many Team(s) and Team can have many Invite(s), then it becomes a Many-to-Many relation and to implement it you need a different table structure with a pivot table invite_team

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM