Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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_Eloquent - Fatal编程技术网

Php Laravel不在工作

Php Laravel不在工作,php,laravel,eloquent,Php,Laravel,Eloquent,我试图使用两个表和一个查找表之间的“belongtomany”关系来定义多对多关系。这些是我的桌子: 遭遇模板(遭遇模板模型) 身份证 模板名称 。。。(更多无关的列) 遭遇形式(遭遇形式模型) 身份证 表格名称 。。。(更多无关的列) 遭遇模板表单(遭遇模板模型) 身份证 模板id 表格编号 在EncounterTemplate模型中,我试图附加一个属于它的表单列表,因此我有一个forms()函数,具有以下内容: public function forms() { retu

我试图使用两个表和一个查找表之间的“belongtomany”关系来定义多对多关系。这些是我的桌子:

遭遇模板(遭遇模板模型)
  • 身份证
  • 模板名称
  • 。。。(更多无关的列)
遭遇形式(遭遇形式模型)
  • 身份证
  • 表格名称
  • 。。。(更多无关的列)
遭遇模板表单(遭遇模板模型)
  • 身份证
  • 模板id
  • 表格编号
在EncounterTemplate模型中,我试图附加一个属于它的表单列表,因此我有一个forms()函数,具有以下内容:

public function forms() {
    return $this->belongsToMany('\App\EncounterForm', 'encounter_templates_forms', 'template_id', 'form_id');
}
但是,它返回一个空对象。我可以通过使用以下方法使其工作:

$forms = \App\EncounterTemplateForm::where("template_id",$this->id)
    ->join("encounter_forms", "encounter_templates_forms.form_id","=","encounter_forms.id")
    ->get();

return $forms;
但我想知道我的关系声明有什么错。如果可以的话,我更愿意用“合适的”拉雷维尔方法。如果您有任何见解,我将不胜感激

编辑:如果运行EncounterTemplate::find(1)->forms()->toSql(),将得到以下查询:

select * from `encounter_forms` inner join `encounter_templates_forms` on `encounter_forms`.`id` = `encounter_templates_forms`.`form_id` where `encounter_templates_forms`.`template_id` = 1

返回预期的结果。。。因此,问题可能更为严重……

从文档中看,你们的关系似乎有点疏远:

…第三个参数是定义关系的模型的外键名称,而第四个参数是要加入的模型的外键名称

因此,参数如下所示:

  • 要链接到的其他模型
  • 联接这些记录的表的名称
  • 联接表中引用当前模型的字段的名称
  • 联接表中引用外部模型的字段的名称
  • 如果您当前正在
    遇到模板
    模型上定义此关系,则第三个参数应为
    模板id
    ,第四个参数为
    表单id

    public function forms() {
        return $this->belongsToMany('App\EncounterForm', 'encounter_templates_forms', 'template_id', 'form_id');
    }
    

    所以在更下游的地方,我动态地调用forms()方法。事实证明,我还需要对其调用->get(),因为它显然是forms()返回查询生成器实例,而不是模型结果。对于未来感兴趣的人,我的工作代码如下所示:

    $record = $model::find($id);
    $include = $request->input("include");
    $result_array = $record->toArray();
    
    if ($include) {
        $includes = json_decode($include);
    
        foreach ($includes as $child_model) {
            $child = $record->$child_model();
            $result_array[$child_model] = $child->get(); //where $child == 'forms'
        }
    }
    
    return $record_array;
    

    感谢您对James进行故障排除的帮助!您的输入帮助我缩小了问题的来源。

    试试这个,它对我有效

    将此添加到您的模型中

    遇到模板模型

    public function encounterTemplateForm()
        {
            return $this->hasMany(EncounterTemplateForm::class);
        }
    
    public function encounterTemplateForm()
        {
            return $this->hasMany(EncounterTemplateForm::class);
        }
    
    public function template()
    {
        return $this->belongsTo(EncounterTemplate::class, 'template_id', 'id');
    }
    
     public function form()
    {
        return $this->belongsTo(EncounterForm::class, 'form_id', 'id');
    }
    
    遭遇形式模型

    public function encounterTemplateForm()
        {
            return $this->hasMany(EncounterTemplateForm::class);
        }
    
    public function encounterTemplateForm()
        {
            return $this->hasMany(EncounterTemplateForm::class);
        }
    
    public function template()
    {
        return $this->belongsTo(EncounterTemplate::class, 'template_id', 'id');
    }
    
     public function form()
    {
        return $this->belongsTo(EncounterForm::class, 'form_id', 'id');
    }
    
    遇到模板模型

    public function encounterTemplateForm()
        {
            return $this->hasMany(EncounterTemplateForm::class);
        }
    
    public function encounterTemplateForm()
        {
            return $this->hasMany(EncounterTemplateForm::class);
        }
    
    public function template()
    {
        return $this->belongsTo(EncounterTemplate::class, 'template_id', 'id');
    }
    
     public function form()
    {
        return $this->belongsTo(EncounterForm::class, 'form_id', 'id');
    }
    

    你是对的,文档表明应该是另一种方式,这是我最初尝试的方式,但不幸的是,我尝试了两种变体,但似乎都不起作用。我已经编辑了我的文章,以反转列名(并且再次运行代码以反转列名,只是为了再次检查我是否疯了)。。。它仍然不起作用。@user3246127尝试预览SQL以查看发生了什么
    EncounterTemplate::find(1)->forms->toSql()这假设您有一个包含表单的模板1。它返回正确的查询,并运行该查询原始返回预期结果,因此问题可能在我的项目下游的某个地方。等我弄明白了,我会更新这个问题的!