2016-07-22 72 views
1

第一次深入Laravel 5我面臨一個問題我不知道如何解決。我有一個表叫查詢與一個查詢類型爲每個。以相反順序查詢屬於類型,即查詢類型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. 我能做什麼錯?

我已經意識到傳遞給方法的字符串是在被查詢的模型上定義的關係函數。但是,如果我更改關係模型的名稱並使用新的關係模型名稱進行查詢,則會得到一個異常,這會鬆散地轉換爲查詢生成器無法找到該方法。

  1. 有沒有什麼特別的我需要做的關係定義方法反映在我使用「with」調用模型運行的查詢上?

您的指導將不勝感激。

回答

1

根據Laravel 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示例。