[英]Dealing with Eloquent relationships in Laravel 5.3
我使用Schema
創建了以下表:
Schema::create('typen', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
});
Schema::create('auktionen', function (Blueprint $table) {
$table->increments('id');
$table->integer('typ')->unsigned();
$table->foreign('typ')->references('id')->on('typen');
});
表typen
僅在包含固定值的情況下創建一次:
Typen
id| name
1 | Typ 1
2 | Typ 2
...
這些值是恆定的,在應用程序生命周期內不會再添加任何值。 因此,我創建的每個Auktion
都應與一個Typ
相關聯。 我為Auktion
創建了模型, Auktion
其視為一對一的關系(對嗎?)。
我想使用這樣的口才查詢來創建具有給定類型的Auktion:
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->associate($thetyp);
並取name
中的Typ
我的Auktion
:
$auktion->typ->name;
目前,我的模型在Auktion
看起來像這樣:
public function typ()
{
return $this->hasOne('App\Typ', 'typ');
}
這是行不通的。 我已經嘗試設置不同的關系,但是我遇到的錯誤代碼范圍很廣,從未定義的方法(associate()
到一個錯誤,在該錯誤中,SQL語句嘗試更新我的typen
表失敗(使用save()
,我真的不想這樣做) )。
有人可以為我澄清這個問題並解釋我必須使用哪種關系嗎?
EDIT1 :如評論中所述,我已經嘗試在Auktion
模型中使用belongsTo
public function typ()
{
return $this->belongsTo('App\Typ', 'typ');
}
導致Undefined property: App\\Auktion::$typ
調用時Undefined property: App\\Auktion::$typ
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->save($thetyp);
並在調用$auktion->typ->name;
在
Trying to get property of non-object
編輯2 :
我只是覺得
echo $auktion->typ()->first()->name;
確實在工作。 但是參考這個答案應該與
echo $auktion->typ->name;
我到底在做什么錯?
編輯3 :
我嘗試使用建議的代碼:
$thetyp = App\Typ::find($typ);
$auktion->typ->save($thetyp);
在導航到該視圖之后,我運行了代碼:
我今天第二次拿到這個
您的第二次編輯很有意義。 您的方法名稱是typ
但這也是列的名稱。 因此,當您使用$auktion->typ()
它實際上是在加載關系。 當您使用$auktion->typ
它將獲取該列的值。
您需要繼續使用圓括號來加載關系,而不需要使用圓括號來獲取列的值,或者將列的名稱更改為更好的名稱(例如typ_id
,這最終是Laravel期望的)並可以避免以后遇到更多類似的麻煩。
這是一些代碼增強功能:
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->save($thetyp);
至:
//Will return null if Model is not found
$thetyp = App\Typ::find(1);
//You actually have to retrieve the relationship, because calling typ()
//will only retrieve the Relationship query
$auktion->typ()->get()->save($thetyp);
問題在於關系是向后定義的。 您需要將Auction設置為belongTo
類型,並將Type更改為hasMany
Auctions。 該語句將顯示為:
“一種類型有很多拍賣。一種拍賣有一種類型”。
這是與遷移有關的課程(英語,對不起,我的德語不好:(所以我只是用英語做了):
-拍賣類:
class Auction extends Model
{
protected $table = 'auction';
public function type()
{
return $this->belongsTo('App\Type');
}
}
-拍賣遷移:
Schema::create('auction', function(Blueprint $table){
$table->increments('id');
$table->integer('type_id')->references('id')->on('type')->nullable();
$table->string('title')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
-類型類:
class Type extends Model
{
protected $table = 'type';
public function auction()
{
return $this->hasMany('App\Auction');
}
}
-類型遷移:
Schema::create('type', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
首先,您可以創建一個Type
對象(或在查詢中插入它),以便我們可以擁有一個與Auction
對象/條目相關的Type
行,並執行以下操作:
//Example of Type obj creation
$type = new Type();
$type->name = 'Type #1';
//Don't forget to save
$type->save();
//OR find/retrieve a Type obj
//Where $id is the id of the Type obj, else the obj will be null
$type = Type::find($id);
$auction = new Auction();
$auction->title = 'Title #1';
$auction->description = 'Test description';
$auction->type_id = $type->id;
//Don't forget to save
$auction->save();
現在,在代碼的稍后部分,每當您使用Auction
對象並且想要檢索關聯的類型(如果有)時,都可以使用:
$type = $auction->type()->get();
這將返回Type
的實例,您將能夠像下面這樣檢索屬性名稱:
$type->name
我希望這有幫助! 讓我知道您是否還有其他問題!
您可以在Auktion模型中嘗試
$thetyp = App\Typ::find(1);
$auktion->typ->save($thetyp);
現在獲取
$auktion->typ->name
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.