Php 拉雷维尔雄辩-一对多-可以';不加入

Php 拉雷维尔雄辩-一对多-可以';不加入,php,laravel,eloquent,Php,Laravel,Eloquent,我使用的是laravel 5.4,有两个模型ParentAccount和ChildAccount, 父母有很多孩子 母公司 <?php namespace App; use Illuminate\Database\Eloquent\Model; class ParentAccount extends Model { // public $timestamps = false; protected $table = 'parent_accounts';

我使用的是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');
    }
}
编辑的代码

public function childs()
  {
      return $this->hasMany('App\ChildAccount', 'parent_id','account_id');
  }
关系函数的格式必须为

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

public function childs()
  {
      return $this->hasMany('App\ChildAccount', 'parent_id','account_id');
  }
关系函数的格式必须为

public function post()
{
   return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
  • 由于
    ParentAccount::find(1)
    return
    null
    ,您得到了对null上的成员函数childs()的
    调用。确保数据库中有id为1的ParentAccount
  • 您需要按如下方式更改密钥顺序:

    public function childs()
    {
        return $this->hasMany('App\ChildAccount', 'parent_id', 'account_id');
    } 
    
  • 由于
    ParentAccount::find(1)
    return
    null
    ,您得到了对null上的成员函数childs()的
    调用。确保数据库中有id为1的ParentAccount
  • 您需要按如下方式更改密钥顺序:

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

  • 首先,您必须添加
    primaryKey
    属性,然后更改关系参数并将反向关系添加到
    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');
          }
      
      }
      

      首先,您必须添加
      primaryKey
      属性,然后更改关系参数并将反向关系添加到
      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');
            }
        
        }
        
        在父模型中

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

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

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

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


        将父模型和子模型中的主键更改为
        account\u id
        而不是
        id
        @Maraboc但是我已经在hasmany函数中定义了它,所以我不需要它,对吗?不,不是这样,因为在中,you have
        Eloquent还将假设每个表都有一个名为id的主键列。您可以定义一个受保护的$primaryKey属性来覆盖此约定。
        @Maraboc yes如果使用find(1),则需要此属性,但是如果使用all(),它也不会起作用<代码>$result=ParentAccount::whereAccountId(1)->with('childs')->get()
    将父模型和子模型中的主键更改为
    account\u id
    而不是
    id
    @Maraboc但是我已经在hasmany函数中定义了它,所以我不需要它,对吗?不,不是这样,因为在中,you have
    Eloquent还将假设每个表都有一个名为id的主键列。您可以定义一个受保护的$primaryKey属性来覆盖此约定。
    @Maraboc yes如果使用find(1),则需要此属性,但是如果使用all(),它也不会起作用<代码>$result=ParentAccount::whereAccountId(1)->with('childs')->get()当我使用$parents=ParentAccount::with('childs')->get()时;为了获取每个父对象及其子对象,childs对象为emptynvm获得它,将account_id声明为primarykey,因此当我使用$parents=ParentAccount::with('childs')->get()时,它的长度受到限制;为了获取每个父对象及其子对象,childs对象为emptynvm获取了它,将account_id声明为primarykey,因此它的长度为limited@JenuRudan这会给你想要的结果,我希望;)谢谢@Tpojka我错过了;)@JenuRudan我希望这会给你想要的结果;)谢谢@Tpojka我错过了;)