Laravel Eloquent - 一对多 - 无法加入

时间:2017-10-22 11:24:42

标签: 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';
    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) - &gt; childs(); 我收到错误虽然所有父母都有孩子,但是在null上调用成员函数childs()

注意:child的parent_id位于父

中的account_id

4 个答案:

答案 0 :(得分:6)

在父模型中

lib

在儿童模特中

<form method="POST" enctype="multipart/form-data">
     <?php 
     $sql="select property_img from img_tbl";

         $q=$conn->query($sql);
           if($q->num_rows>0)
           {
             while($r1=$q->fetch_assoc())
           {
                $property_img1=$r1['property_img'];
             $property_img2=explode(' ',$property_img1);


            echo '

         <div id="shade">
         <div class="col-sm-12 " id="color">
         <div class="col-sm-4 form-group">    

         <div class="row fppadd"> ';

         ?>

          <?php
            foreach($property_img2 as $data){

            echo  '<img class="img-zoom" src="uploads/' . $data . '" width="200" height="150" alt="not fetched">&nbsp;&nbsp;';
            }

            ?> 
             <br></a></span><br>
         </div>
        </div>



      </div>

      </div> <?php
           }
           }?>       
           <br> 






      </form>

答案 1 :(得分:3)

已编辑的代码

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

关系函数必须采用

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

答案 2 :(得分:3)

首先,您必须添加SerialNumber 00330-80000-00000-AA622 SerialNumber 1313311313-13345 属性,然后更改关系参数并将反转关系添加到<StackLayout> <ActionBar title="My App" class="action-bar"></ActionBar> <Label text='hello world'></Label> <button text="Click" (tap)="test()"></button> <test></test> // Its a component </StackLayout>

  • ParentAccount:

    primaryKey
  • ChildAccount:

    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');
    }
}

答案 3 :(得分:1)

  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');
    }