【问题标题】:Call to undefined relationship [users] on model [App\Team] laravel 6在模型 [App\Team] laravel 6 上调用未定义的关系 [users]
【发布时间】:2020-06-20 13:11:04
【问题描述】:

嘿,我是编码和 laravel 方面的新手。我正在尝试在这里构建一个应用程序,我认为我搞砸了,但找不到解决方法。我正在调用模型 [App\ 上的未定义关系 [用户]团队] 例外。我有两张具有多对多关系的表。我想在视图“viewteams.blade.php”中显示用户所属的团队。

我的用户模型

    public function users(){
      return $this->belongsToMany(Team::class);
    }

我的团队模型

  public function teams(){
    return $this->belongsToMany(User::class);
  }

我的路线

Route::get('/viewteams','ViewTeamController@index');

我的 ViewTeamController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Team;
use App\User;

class ViewTeamController extends Controller
{
    public function index()
    {
      $teams = Team::all()->load('users');
      return view('teams.viewteams',compact('teams'));
    }
}

我的观点teams.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header"><h2 style="text-align:center;">Your Teams</h2></div>
                @foreach ($teams as $team)
                  @foreach($team->users as $user)
                    {{$user->org_name}}
                    @endforeach
                @endforeach

                <div class="card-body">

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

【问题讨论】:

  • User 模型与teams 相关。您给出了正确的关系,但函数名称错误。在User.php 表中,您的函数应为teams,在Team.php 文件中,您的函数名称应为users :)

标签: php mysql laravel composer-php show


【解决方案1】:

错误意味着您调用的关系在该模型中不存在。

User 模型与teams 相关。您给出了正确的关系,但函数名称错误。

User.php 表中,您的函数名称应为teams

Team.php文件中,你的函数名应该是users

用户模型

public function teams(){
  return $this->belongsToMany(Team::class,'team_user','users_id','teams_id');
}

团队模型

  public function users(){
    return $this->belongsToMany(User::class,'team_user','teams_id','users_id');
  }

控制器。

public function index()
{
  $teams = Team::with('users')->get();
  return view('teams.viewteams',compact('teams'));
}

【讨论】:

  • 我们摆脱了那个错误,但另一个错误出现了 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team_user.team_id' in 'field list' (SQL: select users.*, team_user.team_id` 作为pivot_team_id, team_user.user_id 作为pivot_user_id 来自@ 987654338@inner join team_user on users.id = team_user.user_id where team_user.team_id in (1))`我猜它与我的数据库有关
  • 这是我的数据透视表迁移 ``` Schema::create('team_user', function (Blueprint $table) { $table->unsignedBigInteger('users_id'); $table->foreign( 'users_id')->references('id')->on('users'); $table->unsignedBigInteger('teams_id'); $table->foreign('teams_id')->references('id') ->on('teams'); $table->primary(array('users_id','teams_id')); $table->index('users_id'); $table->index('teams_id'); $表->时间戳();}); ```
  • 没有更多错误,但我的 pivot_table 中没有数据。为什么会发生这种情况?我想通过关系 laravel 自动做到这一点?
  • 你的team_user表中有数据吗?
猜你喜欢
  • 2020-01-28
  • 2018-05-23
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 2019-12-01
  • 2018-09-13
相关资源
最近更新 更多