2013-03-31 17 views
0

我卡住了一下,我有很多如果其他語句handeling,我將需要更多地使用這個沒有頁面,所以它在視圖中的handeling是不是好主意(並閱讀它不是一個好主意)Laravel最好或有效的方式來處理,如果其他語句

所以我堅持什麼我有2個表。

user 
users_details 

User table存儲基本的登錄信息,並details具有以下字段

user_id 
first_name 
last_name 
company 
location  
experience 
compensation  
about 
gender 
year  
day 
month 
profile_image 

我的關係

用戶模型中的用戶

public function detail() 
{ 
    return $this->has_one('Detail'); 
} 

詳細模型

public function user() 
{ 
    return $this->belongs_to('User'); 
} 

那麼什麼套牢是如何處理的對象與r eturn experience, compensation IM,因爲這些東西我會需要超過一個頁面上,起初,我在我的控制器這樣做

public function get_index($username = null) 
{ 

    $user = User::get_profile($username); 

    if (is_null($user)) return Response::error('404'); 

    switch ($user->detail->experience) { 
    case 1: 
     $user->detail->experience = "No experience"; 
    break; 

    case 2: 
     $user->detail->experience = "Some experience"; 
    break; 

    case 3: 
     $user->detail->experience = "Experienced"; 
    break; 

    case 4: 
     $user->detail->experience = "Very experienced"; 
    break; 

    } 

    switch ($user->detail->compensation) { 
    case 1: 
     $user->detail->compensation = "Any"; 
    break; 

    case 2: 
     $user->detail->compensation = "Depends on Assignment "; 
    break; 

    case 3: 
     $user->detail->compensation = "Paid Assignments Only "; 
    break; 

    case 4: 
     $user->detail->compensation = "Time for Print "; 
    break; 

    } 

    $this->layout->title = 'Profile' . ' ' . $username ; 

    $this->layout->content = View::make('user.profile')->with('user', $user); 

}  

而我知道這是麥芽的想法,我應該拿出來,因爲它不會解決我的問題,不有效,我仍然需要複製。

那麼有人可以告訴我,並以示例和有效的方式來處理這些?

將不勝感激,謝謝

回答

2

好吧,這很奇怪,的確如此。

但是,你有2種方式:

1)定義的答案表,並與您的用戶表連接,所以SQL結果將返回字符串,而不是ID

2)如果你仍然想爲一些理由ID關聯到文字上的代碼級別,你可以做到以下幾點:

詳細模型定義方法,如compensation_str(),這將使此開關,同樣爲經驗研究。並在每個地方,你需要顯示這個文本,你打電話給這個方法($ user-> details-> compensation_str())

+0

你剛剛給了我一個偉大的邏輯思想,謝謝 – Side

+0

我喜歡第二個選項。如果你想要這個邏輯存在,那麼它應該在模型中。我明白你爲什麼要走這條路。 DB使用ints over varchar進行高度優化,並從主數據中分離第三級數據...但使用ORM的IMO實際上是在縮減開發時間/成本,因此在數據庫設計上略微考慮一下。 –

+0

我選擇了第一個選項,因爲我需要能夠通過管理面板編輯這些選項,第一個選項是更好的解決方案 – Side