Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回包含父表中所有子表的数组_Php_Laravel_Eloquent - Fatal编程技术网

Php 返回包含父表中所有子表的数组

Php 返回包含父表中所有子表的数组,php,laravel,eloquent,Php,Laravel,Eloquent,我使用的是Laravel5.7,我有两个表,分别称为“parent_table”和“parent_id,parent_name”,以及“children_table”和“children_id,children_name”。我有两个模型,它们的表名相同,代码如下: 父模型 public function children() { return $this->hasMany('App\children', 'children_id'); } public function ch

我使用的是Laravel5.7,我有两个表,分别称为“parent_table”和“parent_id,parent_name”,以及“children_table”和“children_id,children_name”。我有两个模型,它们的表名相同,代码如下:

父模型

public function children()
{
    return $this->hasMany('App\children', 'children_id');
} 
 public function children()
 {
    return $this
                ->hasMany('App\children',"parent_id")
                ->select(
                    "children_id",
                    "children_nombre",
                    "parent_id"//I wasn't selecting this guy
                )
                //->with("children2") // With this i got the way to concatenate a lot of subchildren map :D
                ;
 }
儿童模型

public function parent()
{
    return $this->belongsTo('App\parent', 'parent_id');
} 
我有一个控制器和这个代码

$data = App\parent::with("children")->get();
return $data;
但它只返回每个“家长”的第一个孩子。我想知道我需要向代码中添加什么来获取每个父级的所有子级


我已经尝试获取所有的“父母”和所有的“孩子”,但这将是对数据库的许多要求。谢谢

如您在示例中所做的那样,将
一起使用将有效。也许您必须在模型的$appends数组中添加“children”

namespace App;
class Parent {
  ...
  protected $appends = ['children'];
  ...
}

当模型转换为数组时,这将附加子结果。您可以执行以下操作:

 @foreach($parents as $parent)
{
  $parent->children();
}
这将从子模型返回一个集合。欲了解更多信息,请访问此处:
几个小时后,我找到了解决办法。我不知道选择外键很重要。。。这就是为什么我的查询不起作用

最终父模型

public function children()
{
    return $this->hasMany('App\children', 'children_id');
} 
 public function children()
 {
    return $this
                ->hasMany('App\children',"parent_id")
                ->select(
                    "children_id",
                    "children_nombre",
                    "parent_id"//I wasn't selecting this guy
                )
                //->with("children2") // With this i got the way to concatenate a lot of subchildren map :D
                ;
 }
最终儿童模型

public function parent()
{
    return $this->belongsTo('App\parent',"parent_id","parent_id");
}
控制器

$data = App\parent::
                select(
                    "parent_id",
                    "parent_nombre"
                )
                ->with("children")
                ->get();

    return $data;

今天我学到了一些新东西,我希望这个答案也能对其他laravel学习者有所帮助,因此,请更正您的帖子,因为这两个函数是混合的。我不确定是否建议迭代一个有说服力的查询,因为它将打开和关闭许多sql语句。我明白,您想用一个查询来完成这一操作吗?嗯,我想最好的办法是像你一样使用“With”。试试这个:Parent::with('children')->find($id);看看这个。急切加载会话必须具有您所需的一切实际上,不需要选择外键。第一种方法不起作用的原因是,在父模型的children()函数中传入了错误的外键“children\u id”。您需要传入将用作子表外键的列,即“parent_id”