2016-04-19 6 views
0

私は小さなチケット予約サービスを行っています。私の目標は、チケットモデルを2つのユーザーインスタンスにリンクすることです。1.チケットを作成したマネージャー2.チケットを所有しているユーザー。Laravel:チケットモデルからユーザーモデルの2つのインスタンスへの2つの外部キー

ユーザー

テーブル

id 
name 
email 
manager 

モデル

public function myticket() { 
return $this->hasMany(App::Ticket); 
} //this is for the ticket owner 

public function createdticket() { 
return $this->hasMany(App::Ticket); 
} //this is for the manager who created the ticket 

チケット

は、私は2つのモデルを持っています

テーブル

id 
ticketcode 

$table->integer(user_id)->unsigned(); //the user who owns the ticket 
$table->foreign('user_id')->references('id')->on('users'); 

$table->integer(manager_id)->unsigned(); //the manager who created 
$table->foreign('manager_id')->references('id')->on('users'); 

モデル

public function user() { 
return $this->belongsTo(App::User); 
} //this is for the ticket owner 

public function createdby() { 
return $this->belongsTo(App::User); 
} //this is for the manager who created the ticket 

どのように私はこの雄弁な対応をするために自分のコードを調整することができますか?

答えて

1

唯一のことは、あなたの関係に正確な列を追加することです。

public function myticket() 
{ 
    return $this->hasMany(App::Ticket, 'user_id'); 
} //this is for the ticket owner 

public function createdticket() 
{ 
    return $this->hasMany(App::Ticket, 'manager_id'); 
} //this is for the manager who created the ticket 

そして、それがあるべきTicketモデルのために:それは、デフォルトでだから

public function user() 
{ 
    return $this->belongsTo(App::User, 'user_id'); 
} //this is for the ticket owner 

public function createdBy() 
{ 
    return $this->belongsTo(App::User,'manager_id'); 
} //this is for the manager who created the ticket 

あなたはそれらの関係でuser_idを無視することができますが、場合にあなたが持っている、それがあるべきUserモデルで

他の名前や複数のユーザーとの関係を読みやすくするには、それらを含める方が良いです

関連する問題