【问题标题】:How to get specific records in database laravel?如何获取数据库laravel中的特定记录?
【发布时间】:2020-06-16 11:26:24
【问题描述】:

我使用 Laravel 开发了一个项目,学生可以从 8:00 AM - 10:00 PM 中选择自己的日程安排,现在在我的 teacher_tbl 中,它的字段名称为 shifts,其数据为 am or pm

我将 8:00 AM - 5:00 PM 标记为 am 并将 1:00 PM - 10:00 PM 标记为 pm,以便我可以轻松获取。现在我的问题是,当学生选择1:00 PM - 5:00 PM 之间的时间时,我怎样才能得到老师,因为......它都在AMPM 之下。

这就是我从选定选项元素中获取学生选定时间的方式。

if ($selectedTime == "8:00") 
    $shift= 'AM';
else if ($selectedTime == "9:00") 
    $shift= 'AM';
...
else if ($selectedTime == "1:00") 
    $shift= ""; //what should I label this?
...
// and so on...

Teacher::where('shifts', $shift)->get();

这是我的问题.. 我应该给 1:00 - 5:00 加上什么标签是上午还是下午。这是我无法理解的部分。

上午教师只在8AM-5PM可用,下午教师1PM-10PM

假设学生选择2PM,它应该是AM&PM教师将获取,但选择6PM应该只获取PM教师。

我很困惑。希望任何人都可以帮助我..

干杯。

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    我假设您正在尝试让一位老师根据学生的选择进行 AM、PM 或两者的轮班。

    如果是这种情况,那么您可以像这样简单地使用whereIn

    // your shift variable here should be an array. ['AM'], ['PM'], or ['AM','PM'] should be the value
    if ($selectedTime == "8:00") 
        $shift= ['AM'];
    else if ($selectedTime == "9:00") 
        $shift= ['AM'];
    ...
    else if ($selectedTime == "1:00") 
        $shift= ['AM','PM']; //what should I label this?
    ...
    // and so on...
    
    $teachers = Teacher::whereIn('shifts', $shift)->get();
    

    另外,作为旁注,我建议不要做很长的 if else 语句,因为它会很快失控,您可以尝试至少使用 Switch case 或使用碳之类的东西进行时间比较。

    【讨论】:

    • 感谢您的回复,我在上面更新了我的问题。希望我的问题很清楚。
    • @Jin 我已经更新了我的答案,请试一试:)
    • 是的,伙计,我还是 Laravel 的新手。我只会更新我的代码。谢谢你的建议:)
    【解决方案2】:
    if ($selectedTime == "8:00") 
        $shift= ['AM'];
    else if ($selectedTime == "9:00") 
        $shift= ['AM'];
    ...
    else if ($selectedTime == "1:00") 
        $shift= ['AM','PM']; //Select both AM and PM
    ...
    // and so on...
    
    Teacher::whereIn('shifts', $shift)->get();
    

    这应该会根据您对问题陈述的理解来解决您的问题。

    乐于助人!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多