Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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 Laravel数据透视表(检索所属的所有项目)_Php_Laravel - Fatal编程技术网

Php Laravel数据透视表(检索所属的所有项目)

Php Laravel数据透视表(检索所属的所有项目),php,laravel,Php,Laravel,我有一个包含字段的项表 我有一个模板表,它使用透视表对字段进行分组 我有一个页面表,其中包含正在使用的模板 我需要检索与页面使用的模板关联的字段列表 $gestionPage->load('template', 'parent'); $page = $gestionPage->get()[0]; $formulaire = Item::find($page->template_id)->with('itemsTemplate')->get()

我有一个包含字段的项表 我有一个模板表,它使用透视表对字段进行分组 我有一个页面表,其中包含正在使用的模板

我需要检索与页面使用的模板关联的字段列表

    $gestionPage->load('template', 'parent');
    $page = $gestionPage->get()[0];
    $formulaire = Item::find($page->template_id)->with('itemsTemplate')->get();

    foreach($formulaire as $k=>$v){
        $data[] = $v->nom;
    }
    dd($data);
因此,数据包含所有字段(而不仅仅是属于正确模板的字段)

2=>“所见即所得”不是模板的一部分

它应该输出

   array:2 [▼
     0 => "Titre"
     1 => "Texte"
   ]
我不明白pivot系统是怎么工作的

在项目模型中

public function itemsTemplate()
{
    return $this->belongsToMany(Template::class);
}
public function items()
{
    return $this->belongsToMany(Item::class);
}
在项目模型中

public function itemsTemplate()
{
    return $this->belongsToMany(Template::class);
}
public function items()
{
    return $this->belongsToMany(Item::class);
}

如何轻松检索模板id拥有的项目列表?

我对您的代码做了一些更改。看看现在能不能用。从这个链接可以了解在Laravel中多对多关系的工作原理

$gestionPage->load('template','parent');
$page=$gestionPage->get()[0];
$formulaire=Item::find($page->template_id)->templates()->get();
dd($公式化);
//将其从itemsTemplate重命名为templates-因为它是复数
公共函数模板()
{
返回$this->belongstomy(模板::类);
}
公共职能项目()
{
返回$this->belongtomany(Item::class);
}
在这里尝试一下:

public function templates() 
{
    return $this->belongsToMany(Template::class)->withPivot(array $fields);
}

public function items()
{
    return $this->belongsToMany(Item::class)->withPivot(array $fields)
}

然后,您可以访问如下透视表字段:
$item->pivot->field

谢谢!返回$this->belongToMany(Item::class)->withPivot(数组('template_id','Item_id'));我成功了!