Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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_Laravel 5_Laravel 5.4 - Fatal编程技术网

如何从中间表Laravel获取数据?

如何从中间表Laravel获取数据?,laravel,laravel-5,laravel-5.4,Laravel,Laravel 5,Laravel 5.4,如何通过模型从第三个表中获取数据 我有模型原型和原型场模型 其中原型为: 其中PrototypeField是: 还有一个包含字段名称的表字段: id | name 如何通过模型“Prototype”从表字段中获取名称,其中Prototype与PrototypeField相对: 因此,Prototype可以有一个或多个PrototypeField。您的Prototype模型内部应该有一个关系,它指定了应该与之相关的字段,例如: public function fields() { re

如何通过模型从第三个表中获取数据

我有模型原型和原型场模型

其中原型为:

其中PrototypeField是:

还有一个包含字段名称的表字段:

id | name 
如何通过模型“Prototype”从表字段中获取名称,其中Prototype与PrototypeField相对:


因此,Prototype可以有一个或多个PrototypeField。

您的Prototype模型内部应该有一个关系,它指定了应该与之相关的字段,例如:

public function fields()
{
    return $this->belongsToMany('App\Fields'); // Change to location of fields model
}
您的字段模型还应包含一个关系,以指定它与哪些原型相关,例如:

public function prototypes()
{
    return $this->belongsToMany('App\Prototypes'); // Change to location of prototype model
}
设置这些字段后,您将能够使用以下选项选择原型所属的字段:

Prototype::first()->fields; //This selects the first prototype and gets the associated fields.
与之相反的是:

Fields::first()->prototypes //This selects the first field and get associated prototypes.

通过阅读有关Laravel模型的文档,您可以找到大量信息-

您是否在模型中设置了关系?您能否解释一下在这种情况下如何设置关系ship ui?
public function prototypes()
{
    return $this->belongsToMany('App\Prototypes'); // Change to location of prototype model
}
Prototype::first()->fields; //This selects the first prototype and gets the associated fields.
Fields::first()->prototypes //This selects the first field and get associated prototypes.