如何简化Laravel ORM

时间:2016-11-08 10:04:47

标签: php laravel orm laravel-5.2

打扰一下,这是一个如此愚蠢的问题..

我使用Controller,有很多ORM,我想简化这段代码。

我的控制器

public function news(Request $request)
{
    $history = Recruitments_status::where('recruitments_status.status',1)->get();
    $history_a =  Recruitments_status::where('recruitments_status.status',2)->get();
    $history_b = Recruitments_status::where('recruitments_status.status',3)->get();  
    $history_c = Recruitments_status::where('recruitments_status.status',4)->get(); 
    $history_d = Recruitments_status::where('recruitments_status.status',5)->get(); 
    $history_e = Recruitments_status::where('recruitments_status.status',6)->get(); 

    return view('pl_sidebar/news',[
          'history' => $history,
          'history_a' => $history_a,
          'history_b' => $history_b,
          'history_c' => $history_c,
          'history_d' => $history_d,
          'history_e' => $history_e
    ]);
}

如何简化此ORM代码?

2 个答案:

答案 0 :(得分:3)

使用- (void)viewDidLoad{ [super viewDidLoad]; //UISwitch work at viewDidload [self.mySwitch addTarget:self action:@selector(stateChanged:) forControlEvents:UIControlEventValueChanged]; } - (void)stateChanged:(UISwitch *)switchState{ NSLog(@"current calendar: %@", [[NSCalendar currentCalendar] calendarIdentifier]); //get the date today NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setCalendar:[NSCalendar currentCalendar]]; NSString *dateToday = [formatter stringFromDate:[NSDate date]]; NSLog(@"Today Date is: %@", [NSDate date]); self.myLabel.text=dateToday; if ([switchState isOn]) { //[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US@calendar=japanese"]]; [formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese]]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSString *dateToday = [formatter stringFromDate:[NSDate date]]; UILabel *dtDate = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)]; [dtDate setText:dateToday]; self.myLabel.text =dateToday; NSString *locale = [[NSLocale currentLocale] localeIdentifier]; NSLog(@"current date is: %@", dateToday); NSLog(@"current locale: %@", locale); } else { //[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; [formatter setCalendar:[NSCalendar autoupdatingCurrentCalendar]]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSString *dateToday = [formatter stringFromDate:[NSDate date]]; UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)]; [myLabel setText:dateToday]; self.myLabel.text =dateToday; // NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // [formatter setDateStyle:NSDateFormatterMediumStyle]; NSString *locale = [[NSLocale currentLocale] localeIdentifier]; NSLog(@"current locale: %@", locale); NSLog(@"%@", [formatter stringFromDate: [NSDate date]]); } }

whereIN

所以,

  ->whereIn('id', [1, 2, 3])->get();

答案 1 :(得分:3)

您的代码会生成多个查询,这是不好的做法。你可以得到一个集合:

 $history = Recruitments_status::whereIn('recruitments_status.status',[1,2,3,4,5,6])->get();

然后在Blade模板中使用此集合:

public function news(Request $request)
{
    return view('pl_sidebar/news', [
        'history' => Recruitments_status::whereIn('status', [1, 2, 3, 4, 5, 6])
    ]);
}