简体   繁体   English

Laravel最好或最有效的方法来处理if语句

[英]Laravel best or effective way to handle if else statement

i am stuck a bit, i have lot of if else statement handeling, and i will need to use this in more the none page, so handeling it in the view is not a good idea (and read about it its not a good idea) 我被困了一点,我有很多if else声明handeling,我将需要在更多的无页面中使用它,所以在视图中处理它并不是一个好主意(并阅读它不是一个好主意)

So what im am stuck with i have 2 tables. 所以我被困在哪里我有2张桌子。

user
users_details

User table stores basic login information and and the user details has the following fields User table存储基本登录信息,用户details包含以下字段

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

my relations 我的关系

User model 用户模型

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

Details model 细节模型

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

So what im stuck with is how to handle the object to r eturn experience, compensation because those what i will need on more than one page, at first i did this in my controller 所以我坚持的是如何处理对象的eturn experience, compensation因为那些我需要在多个页面上,起初我在我的控制器中做到这一点

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

}    

And i know this is the worts idea i should come up with because it wont solve my problem, not effective and i still need to duplicate. 而且我知道这是我应该想出的那个短头脑的想法,因为它不会解决我的问题,没有效果,我仍然需要复制。

So could someone show me and example and effective way to handle these? 那么有人可以向我展示并以有效的方式处理这些问题吗?

Would be grateful, thank you 不胜感激,谢谢

Well, this is weird, indeed. 嗯,这确实很奇怪。

But, you have 2 ways: 但是,你有两种方式:

1) Define tables for answers and join with your user table, so SQL result will return string, not ID 1)定义表的答案并与用户表连接,因此SQL结果将返回字符串,而不是ID

2) If you still want for a some reason to associate ID to Text on code level you can do following: 2)如果您仍然希望出于某种原因在代码级别将ID与Text相关联,则可以执行以下操作:

Define method in details model, like compensation_str() which will make this switch, same for expierence. 在详细模型中定义方法,例如,使用此开关的compensation_str(),expierence相同。 And in each place you need to display this text you call this method ($user->details->compensation_str()) 在每个地方你需要显示这个文本你称之为这个方法($ user-> details-> compensation_str())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM