Laravel 5有一种关系不起作用

时间:2016-07-22 09:46:15

标签: php laravel-5 eloquent

第一次进入Laravel 5我遇到了一个问题,我不知道如何解决。我有一个名为查询的表格,每个表格都有一个查询类型。按相反顺序查询属于类型,即查询类型

以下是定义这些表的迁移类的部分内容:

<?php
  //Enquiry Table
  Schema::create('enquiries', function (Blueprint $table) {
        /*
         * AutoIncrement Field
         */
        $table->increments('id');

        $table->string('name');
        $table->integer('enquiry_type_id')->unsigned();
        $table->boolean('active')->default(1);

        /*
         * ForeignKey Definition(s)
         */
        $table->foreign('enquiry_type_id')->references('id')->on('enquiry_types');

        /*
         * DateTime Fields {created_at, updated_at}
         */
        $table->timestamps();

    });

   //EnquiryType
   Schema::create('enquiry_types', function (Blueprint $table) {
        /*
         * AutoIncrement Field
         */
        $table->increments('id');

        $table->string('name');
        $table->boolean('active')->default(1);

        /*
         * DateTime Fields {created_at, updated_at}
         */
        $table->timestamps();
    });

然后我继续在相关模型上定义这些表的关系。

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Model\EnquiryType;

class Enquiry extends Model
{
  /*
   * Fields that we can mass-assign
   */
   protected $fillable = ['name'];

   /**
    * Get the enquiry type record associated with the enquiry.
    */
   public function type()
   {
      return $this->hasOne(EnquiryType::class);
   }
}

 <?php

 namespace App\Model;

 use Illuminate\Database\Eloquent\Model;
 use App\Model\Enquiry;

 class EnquiryType extends Model
 {
   /*
    * Fields that we can mass-assign
    */
   protected $fillable = ['name', 'enquiry_type_id'];

   /**
   * Get the enquiry record associated with the enquiry type.
   */
   public function enquiry()
   {
     return $this->belongsTo(Enquiry::class);
   }
}

现在获取相关查询的查询类型,但查询相关查询类型。

在我的查询课程中,我选择使用类型方法名称,因为它有意义地说“让我查询类型”而不是“让我查询查询类型”。

以下查询有效:

<?php var_dump(App\Model\EnquiryType::with('enquiry')->get());

但是这个没有:

 <?php var_dump(App\Model\Enquiry::with('type')->get());

以下是我从第二个查询得到的内容:

Illuminate\Database\Eloquent\Collection {#672
 all: [
   App\Model\Enquiry {#669
     id: "1",
     name: "Customer Service",
     enquiry_type_id: "1",
     active: "1",
     created_at: "2016-07-22 07:43:48",
     updated_at: "2016-07-22 07:43:48",
     type: null,
   },
 ],

}

  1. 我能做错什么?
  2. 我已经意识到传递给方法的字符串是在被查询的模型上定义的关系函数。但是,如果我使用新的关系模型名称更改关系模型和查询的名称,则会得到一个异常,该异常会松散地转换为查询构建器无法找到该方法。

    1. 我需要做些什么特别的事情才能让关系定义方法反映在我使用“with”调用对模型运行的查询中?
    2. 您的指导将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

根据Laravel Eloquent惯例:

  

Eloquent通过检查名称来确定默认外键名称   关系方法和用_id

后缀方法名称

因此,在您的type方法中,默认外键将是以下用于加入/建立关系(因为您没有明确提供):

public function type()
{
    return $this->hasOne(
        EnquiryType::class,
        'type_id', // Laravel will use this by default
        'id'      // Laravel will use this by default
    );
}

由于您的字段名称与Laravel不同,Laravel正在使用约定(方法名称:type_id作为后缀,变成type_id)来制作外键。因此,您必须明确告诉它,在您的情况下,它应该是:

public function type()
{
    return $this->hasOne(EnquiryType::class, 'enquiry_type_id', 'id');
}

在这种情况下,第三个(id)参数是可选的,因为它符合命名约定。查看Laravel网站上的One To One示例。

相关问题