[英]Laravel 5.6.17 Model hasOne over multiple tables
我對Laravel還是很陌生,現在我正嘗試將以前的應用程序的一部分從一個小型的自編寫框架移至Laravel。 地址簿是多語言的,因此表結構稍微復雜一些。
這是我的源代碼:
namespace App\Http\Controllers;
use App\AddressBook as AB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class AddressBookController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$entries = AB::all();
return view('addressBook')->with([
'class' => __CLASS__,
'function' => __FUNCTION__,
'line' => __LINE__,
'entries' => $entries,
]);
}
}
型號AddressBook.php
namespace App; use Illuminate\\Database\\Eloquent\\Model; class AddressBook extends Model { protected $table = 'address'; protected $primaryKey = 'address_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; protected $searchable = [ 'columns' => [ 'address.address_surname' => 10, 'address.address_company' => 5, 'address.address_vatid' => 2, ], ]; public function country() { return $this->hasOne('country', 'country_id', 'country_id'); } public function addresstype() { return $this->hasOne('addresstype', 'addresstype_id', 'addresstype_id'); } }
模型Country.php
namespace App; use Illuminate\\Database\\Eloquent\\Model; class Country extends Model { protected $table = 'country'; protected $primaryKey = 'country_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; public function translation() { return $this->hasOne('translations', 'translations_id', 'translations_id'); } public function addressbook() { return $this->belongsTo('address', 'country_id', 'country_id'); } }
型號地址類型
namespace App; use Illuminate\\Database\\Eloquent\\Model; class AddressType extends Model { protected $table = 'addresstype'; protected $primaryKey = 'addresstype_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; public function translation() { return $this->hasOne('translations', 'translations_id', 'translations_id'); } public function addressbook() { return $this->belongsTo('address', 'addresstype_id', 'addresstype_id'); } }
模型Translation.php
namespace App; use Illuminate\\Database\\Eloquent\\Model; class Translation extends Model { protected $table = 'translations'; protected $primaryKey = 'translations_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; public function country() { return $this->belongsTo('country', 'translations_id', 'translations_id'); } public function addresstype() { return $this->belongsTo('addresstype', 'translations_id', 'translations_id'); } }
請求“ $ entries = AB :: all();” 通常可以正常工作,但是我得到了ID,也許我在這里完全錯了,但我認為來自外鍵的數據將被相應的模型替換(如果配置正確)。 所以我的問題是:
一種。 我在配置過程中犯了一個錯誤,如果是,錯誤在哪里?
要么
灣 我用對象替換id的假設是完全錯誤的?
提前致謝! 史蒂夫
Laravel Eloquent模型不會用關系數據替換活動記錄的外鍵,它只會追加一個新屬性,該屬性的名稱與關聯類的方法相同,並且在該屬性中,它會將結果查詢的所有Model實例放入該屬性中,僅當您訪問該屬性時,它才稱為Eager Loading。
$addressBook = App\AddressBook::find(1); //this only will return the active record with the id 1 and nothig more.
$addressBook->country; // The property "country" does not exist in the AddressBook Classs but eloquent models will return a "fake" property with the value of the result of the query by the method with the same name (only if the method returns an Eloquent Relation).
這種雄辯的行為是自然的,並且是最小化查詢數量的一種非常聰明的方法,雄辯的將永遠不會加載不需要的關系。
如果要在從數據庫中檢索模型的同時加載模型中的關系,則需要明確指定要加載的關系。
$addressBook = App\AddressBook::with(['country', 'addresstype', 'anotherRelation'])->get(); // this will retrive all the addressBook models and in each one will attach the relations specified.
[編輯]另外,您還必須將相關模型類的整個名稱空間放置在關聯方法中,因此需要像下面這樣替換:
class Translation extends Model
{
protected $table = 'translations';
protected $primaryKey = 'translations_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
// ****** You need to put the entire namespace of the Model class
public function country() {
return $this->belongsTo('App\Country', 'translations_id', 'translations_id');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.