2016-09-27 63 views
0

我有以下表使用Schema小號創建的:在Laravel與鋒關係處理5.3

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:

$thetyp = App\Typ::where("id", "=", 1)->first(); 
    $auktion->typ()->associate($thetyp); 

,並獲取我的AuktionTypname

$auktion->typ->name; 

目前我的模型看起來像這樣的Auktion

public function typ() 
    { 
    return $this->hasOne('App\Typ', 'typ'); 
    } 

這是行不通的。我已經嘗試設置不同的關係,但(使用save()的時候 - 我真的不希望)我只是在不同的錯誤代碼,從不確定的方法(associate()的錯誤,其中一個SQL語句失敗嘗試更新我的typen表結束。

有人能澄清這個問題我解釋一下我必須使用哪種關係?

EDIT1:作爲調用

$thetyp = App\Typ::where("id", "=", 1)->first(); 
$auktion->typ()->save($thetyp); 

Trying to get property of non-object 
調用 $auktion->typ->name;時,當評論我已經在 Auktion模型

public function typ() 
    { 
    return $this->belongsTo('App\Typ', 'typ'); 
    } 

導致Undefined property: App\Auktion::$typ使用belongsTo試圖提到

EDIT2

我只是想通

echo $auktion->typ()->first()->name; 

確實工作。 But referring to this answer這應該是相同的

echo $auktion->typ->name; 

我究竟做錯了什麼?

EDIT3

我使用建議的代碼嘗試:

$thetyp = App\Typ::find($typ); 
    $auktion->typ->save($thetyp); 

後,我導航到該視圖ehere我跑我得到這個代碼:

enter image description here

我今天第二次得到這個,從哪裏冒出來

+1

請看我的回答。希望能幫助到你! – idelara

回答

1

下面是一些代碼增強:

$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); 

的問題是,這種關係被定義倒退。您需要拍賣belongTo類型,並將類型更改爲hasMany拍賣。聲明如下:

「一種類型有很多拍賣,一種拍賣有一種類型」。

這裏是班(英語,對不起,我的德語不好:(所以我只是做了英文)與遷移:

-Auction類:

class Auction extends Model 
{ 
    protected $table = 'auction'; 

    public function type() 
    { 
     return $this->belongsTo('App\Type'); 
    } 
} 

-Auction遷移:

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(); 

}); 

-Type類:

class Type extends Model 
{ 
    protected $table = 'type'; 

    public function auction() 
    { 
     return $this->hasMany('App\Auction'); 
    } 
} 

-Type遷移:

Schema::create('type', function(Blueprint $table){ 

     $table->increments('id'); 
     $table->string('name'); 

     $table->timestamps(); 
}); 

首先,你可以創建一個Type對象(或查詢插入),所以我們可以有一個Type行,我們可以涉及到一個Auction對象/進入,然後執行以下:

//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

我希望這有助於!如果您還有其他問題,請告訴我!

0

您可以在Auktion模型試試這個

$thetyp = App\Typ::find(1); 
$auktion->typ->save($thetyp); 

現在取

$auktion->typ->name 
+0

查看我編輯的問題 – 4ndro1d

+0

刪除括號。試試$ auktion-> typ-> save($ thetyp); – Vikash

+0

'未定義的財產:App \ Auktion :: $ typ' – 4ndro1d

1

你的第二個編輯,使有很大的意義。您的方法名稱是typ,但這也是該列的名稱。所以當你使用$auktion->typ()它實際上是加載關係。當您使用$auktion->typ時,它會抓取該列的值。

您需要繼續以這種方式使用括號加載關係並且沒有括號來獲取列的值,或者您可以將列的名稱更改爲更好的東西,例如typ_id,這最終是Laravel希望它能夠並且應該讓你免於更多類似的麻煩。