【问题标题】:Why BelongsTo() isn't working in Laravel 6?为什么 BelongsTo() 在 Laravel 6 中不起作用?
【发布时间】:2019-09-13 09:25:01
【问题描述】:

我有两个模型 UserAccountType

class User extends Authenticatable
{
    (...)

    public function accountType()
    {
        return $this->belongsTo('App\AccountType', 'account_type_id', 'id');
    }
}
class AccountType extends Model
{
  protected $table = 'account_types';
  // protected $primaryKey = 'id';

  public function users()
  {
      return $this->hasMany('App\User');
  }
}

我的数据库:

        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('account_type_id')->unsigned();
            $table->foreign('account_type_id')->references('id')->on('account_types');
            $table->string('full_name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
        Schema::create('account_types', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('account_type_name');
        });

当我使用 hasMany 时 - 一切都像魅力一样,但是当我尝试使用 $user->accountType->account_type_name 时它不起作用。我用来打印用户的循环:

                @foreach ($users as $user)
                  <tr>
                    <td>{{ $user->full_name }}</td>
                    <td>
                      <a href="#" class="badge badge-pill badge-warning">No team</a>
                    </td>
                    <td>
                      xyz
                    </td>
                    <td> {{ $user->accountType->account_type_name }} </td>
                    <td><a href="mailto:{{ $user->email }}">{{ $user->email }}</a></td>
                    <td>{{ $user->created_at }}</td>
                  </tr>
                @endforeach

还有错误。当我注释掉 $user-&gt;accountType-&gt;account_type_name 时,它工作正常。

ErrorException
Undefined offset: 1

详情在这里 - https://flareapp.io/share/jKPgOZPa

提前谢谢你!

【问题讨论】:

  • 你遇到了什么错误?
  • @DilipHirapara 我已将这些添加到我的问题中。对不起:)
  • 用户表中是否所有用户都有账户类型ID?
  • 是的,他们默认得到它。
  • 我不是说那是一个错误,但你能不能改变并检查一下,如果 1) accountType() 名称应该是小的 'account_type()' 2) 在 AccountType 模型中 return $this-&gt;hasMany('App\User','account_type_id', 'id');

标签: php laravel laravel-6


【解决方案1】:

你需要用蛇皮而不是骆驼皮:

<td> {{ $user->account_type->account_type_name }} </td>

【讨论】:

  • 没有真正的帮助。仅在模型和刀片.php 中也尝试在刀片.php 中使用蛇盒
  • 似乎某些用户没有有效的帐户类型试试这个:&lt;td&gt; {{ $user-&gt;account_type-&gt;account_type_name ?? '' }} &lt;/td&gt; 看看是否有任何用户想出了空的帐户类型
  • 这正是我现在所拥有的。问题是我有两个用户,每个用户都有有效的user_type_id
  • 什么user_type_id 是有效的,但你在用户模型中与account_type_id 建立关系???
  • 这里有错字;当然你是对的,我的意思是account_type_id
【解决方案2】:

看起来您定义了错误的关系。你的用户模型应该是这样的:

class User extends Authenticatable
{
    (...)

    public function accountType()
    {
        return $this->hasOne('App\AccountType', 'account_type_id', 'id');
    }
}

账户类型模型应该是这样的:

class AccountType extends Model
{
     protected $table = 'account_types';
    // protected $primaryKey = 'id';

    public function user()
    {
       return $this->belongsTo('App\User');
    }
}

【讨论】:

  • 这样我们就建立了一对一的关系,我希望实现一个account_type可以有多个用户,一个用户只有一个account_type。
  • 要让它工作$user-&gt;accountType-&gt;account_type_name,你需要像我提到的那样定义一个关系。
【解决方案3】:

我已将&lt;td&gt; {{ $user-&gt;accountType-&gt;account_type_name }} &lt;/td&gt; 替换为&lt;td&gt; {{ App\User::find($user-&gt;id)-&gt;accountType-&gt;account_type_name }} &lt;/td&gt;,现在一切正常。

【讨论】:

    【解决方案4】:

    你可以试试这个..我的工作正常..

    $user->accountType->first() ->account_type_name
    

    【讨论】:

      猜你喜欢
      • 2021-06-01
      • 2021-01-06
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多