Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
Laravel 拉威尔收集后无法展平阵列->;忘记_Laravel_Orm_Eloquent_Relational Database - Fatal编程技术网

Laravel 拉威尔收集后无法展平阵列->;忘记

Laravel 拉威尔收集后无法展平阵列->;忘记,laravel,orm,eloquent,relational-database,Laravel,Orm,Eloquent,Relational Database,我在Laravel集合的循环中有一个循环,有时我需要从第二个循环集合中删除一些对象。这是密码 public function remove_if_found($id) { $all_groups = Group::all(); $all_groups->load('templates'); foreach ($all_groups as $group) { foreach ($group->templates as $key =>

我在Laravel集合的循环中有一个循环,有时我需要从第二个循环集合中删除一些对象。这是密码

public function remove_if_found($id)
{
    $all_groups = Group::all();
    $all_groups->load('templates');

    foreach ($all_groups as $group)
    {
        foreach ($group->templates as $key => $template)
        {
            if($template->id = $id)
            {
                $group->templates->forget($key);
            }
        }
    }

    return $all_groups;
}
问题在于,组->模板的集合从简单(非assoc)数组变成了对象。下面是一个响应的示例

我试图展平$group->templates->flant(),但在最终的响应中,模板仍然是对象,而不是数组

这个测试很有效

    ...
    foreach ($all_groups as $group)
    {
        foreach ($group->templates as $key => $template)
        {
            if($template->id = $id)
            {
                $group->templates->forget($key);
            }
        }

        return $group->templates->flatten()//This code works i get fluttened array
    }
但最后一个变量仍然返回me对象而不是数组

    $all_groups = Group::all();
    $all_groups->load('templates');

    foreach ($all_groups as $group)
    {
        foreach ($group->templates as $key => $template)
        {
            if($template->id = $id)
            {
                $group->templates->forget($key);
            }
        }

        $group->templates->flatten()//Use flatten here
    }

    return $all_groups;//Templates are returned not as an array but still as an object (Same variant as on attached image)
}
使用
values()
重置键,使用
setRelation()
替换关系:

如果找到公共函数,则删除($id)
{
$all_groups=Group::all();
$all_groups->加载(“模板”);
foreach($group作为$group的所有组)
{
foreach($group->templates as$key=>$template)
{
如果($template->id=$id)
{
$group->setRelation('templates',$group->templates->forget($key)->values());
}
}
}
返回$all_组;
}
您也可以使用
except()
代替
forget()

如果找到公共函数,则删除($id)
{
$all_groups=Group::all();
$all_groups->加载(“模板”);
foreach($group作为$group的所有组)
{
$group->setRelation('templates',$group->templates->except($id));
}
返回$all_组;
}

是的,它可以工作,现在一切都好了,我不明白setRelation做什么,现在就用谷歌搜索,谢谢)它在
模型::$relations
属性中设置关系的值。它类似于“临时”关系?不,它会替换现有值。对于属性,您可以使用类似于
$group->name=strtolower($group->name)的内容。对于关系,这需要
setrelations()
。谢谢你,乔纳斯)