【问题标题】:Laravel Eloquent - One to Many - Can't joinLaravel Eloquent - 一对多 - 无法加入
【发布时间】:2018-04-03 01:33:47
【问题描述】:

我使用的是 laravel 5.4,有两个模型 ParentAccount 和 ChildAccount, 父母有很多孩子

父母

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ParentAccount extends Model
{
    //
    public $timestamps = false;
    protected $table = 'parent_accounts';
    protected $fillable = [
        'name', 'account_id'
    ];
    public function childs()
    {
        return $this->hasMany('App\ChildAccount','account_id', 'parent_id');
    }
}

孩子

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ChildAccount extends Model
{
    //
    public $timestamps = false;
    protected $table = 'child_accounts';
    protected $fillable = [
        'name', 'account_id','parent_id'
    ];
}

当我使用 echo ParentAccount::find(1)->childs(); 尽管所有父母都有孩子,但我在 null 上调用成员函数 childs() 时收到错误消息

注意:child 有 parent_id,它是 parent 中的 account_id

【问题讨论】:

  • 将父模型和子模型的主键改为account_id而不是id!!
  • @Maraboc 但我已经在 hasmany 函数中定义了它,所以我不需要它,对吧?
  • 不,事实并非如此,因为在documentation 你有Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention.
  • @Maraboc 是的,如果使用 find(1),那将是必需的,但如果使用 all(),它也不起作用!
  • $result = ParentAccount::whereAccountId(1)-&gt;with('childs')-&gt;get();

标签: php laravel eloquent


【解决方案1】:

编辑代码

public function childs()
  {
      return $this->hasMany('App\ChildAccount', 'parent_id','account_id');
  }

关系函数必须是格式

public function post()
{
   return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}

【讨论】:

  • 当我使用 $parents= ParentAccount::with('childs')->get();为了让每个父母都有它的孩子,孩子的对象是空的
  • nvm 知道了,将 account_id 声明为主键,所以它的长度是有限的
【解决方案2】:
  1. 你得到了Call to a member function childs() on null,因为ParentAccount::find(1)返回null。确保您的数据库中有 id=1 的 ParentAccount。
  2. 您需要像这样更改键顺序:

    public function childs()
    {
        return $this->hasMany('App\ChildAccount', 'parent_id', 'account_id');
    } 
    

【讨论】:

    【解决方案3】:

    首先您必须添加primaryKey 属性,然后更改关系参数并将revers 关系添加到ChildAccount

    • 家长账户:

      <?php
      
      namespace App;
      
      use Illuminate\Database\Eloquent\Model;
      
      class ParentAccount extends Model
      {
      
          protected $primaryKey = 'account_id';
      
          public $timestamps = false;
          protected $table = 'parent_accounts';
          protected $fillable = [
              'name', 'account_id'
          ];
          public function childs()
          { 
             //                                        'foreign_key', 'local_key'
              return $this->hasMany('App\ChildAccount', 'parent_id', 'account_id');
          }
      }
      
    • 子帐户:

      <?php
      
      namespace App;
      
      use Illuminate\Database\Eloquent\Model;
      
      class ChildAccount extends Model
      {
          protected $primaryKey = 'account_id';
      
          public $timestamps = false;
          protected $table = 'child_accounts';
          protected $fillable = [
              'name', 'account_id','parent_id'
          ];
      
          public function parent() {
              //                                           foreign_key, 'other_key'
              return $this->belongsTo('App\ParentAccount', 'parent_id', 'account_id');
          }
      
      }
      

    这样做你可以得到孩子:

    ParentAccount::find(1)->childs;
    

    【讨论】:

    • @JenuRudan 这会给你我希望的结果;)
    • 谢谢@Tpojka 我错过了;)
    【解决方案4】:

    在父模型中

    public function childs(){
     return $this->hasMany(ChildAccount::class, 'parent_id' , 'account_id');
    }
    

    在子模型中

    public function parent(){
     return $this->belongsTo(ParentAccount::class, 'parent_id' , 'account_id');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2013-02-09
      相关资源
      最近更新 更多