• <i id='vDmpc'><tr id='vDmpc'><dt id='vDmpc'><q id='vDmpc'><span id='vDmpc'><b id='vDmpc'><form id='vDmpc'><ins id='vDmpc'></ins><ul id='vDmpc'></ul><sub id='vDmpc'></sub></form><legend id='vDmpc'></legend><bdo id='vDmpc'><pre id='vDmpc'><center id='vDmpc'></center></pre></bdo></b><th id='vDmpc'></th></span></q></dt></tr></i><div id='vDmpc'><tfoot id='vDmpc'></tfoot><dl id='vDmpc'><fieldset id='vDmpc'></fieldset></dl></div>
  • <legend id='vDmpc'><style id='vDmpc'><dir id='vDmpc'><q id='vDmpc'></q></dir></style></legend>

    <tfoot id='vDmpc'></tfoot>

      <small id='vDmpc'></small><noframes id='vDmpc'>

        <bdo id='vDmpc'></bdo><ul id='vDmpc'></ul>
      1. Laravel - 使用 whereHas 获取最后一行

        时间:2023-09-24
          <i id='unc5x'><tr id='unc5x'><dt id='unc5x'><q id='unc5x'><span id='unc5x'><b id='unc5x'><form id='unc5x'><ins id='unc5x'></ins><ul id='unc5x'></ul><sub id='unc5x'></sub></form><legend id='unc5x'></legend><bdo id='unc5x'><pre id='unc5x'><center id='unc5x'></center></pre></bdo></b><th id='unc5x'></th></span></q></dt></tr></i><div id='unc5x'><tfoot id='unc5x'></tfoot><dl id='unc5x'><fieldset id='unc5x'></fieldset></dl></div>
          <legend id='unc5x'><style id='unc5x'><dir id='unc5x'><q id='unc5x'></q></dir></style></legend>
            • <tfoot id='unc5x'></tfoot>

                <tbody id='unc5x'></tbody>
                <bdo id='unc5x'></bdo><ul id='unc5x'></ul>

                  <small id='unc5x'></small><noframes id='unc5x'>

                  本文介绍了Laravel - 使用 whereHas 获取最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试获取最后一次用户活动的时间created_at",我有模型 UserUserActivity.
                  我想获取最后一个用户活动并检查该用户的最后一个活动是否是 3 天才能发送通知,

                  I am trying to get the time "created_at" for the last user activity, I have the model User, and UserActivity.
                  I want to get the last user activity and check if the last activity of this user is 3 days to send notification,

                  User.php

                  <?php
                  
                  namespace App;
                  
                  use IlluminateDatabaseEloquentModel;
                  class User extends Model
                  {
                      public function activites()
                      {
                          return $this->hasMany(Activty::class);
                      }
                  
                  }
                  

                  Activity.php

                  <?php
                  
                  namespace App;
                  
                  use IlluminateDatabaseEloquentModel;
                  use CarbonCarbon;
                  
                  class Activity extends Model
                  {
                      function function user(){
                          return $this->belongsTo(User::class);
                      }
                  }
                  

                  控制器

                  $latest_activites = User::whereHas("activites",function($query){
                              $query->where("created_at",">=",Carbon::now()->subDays(3));
                          });
                  $latest_activites = $latest_activites->get();
                  

                  推荐答案

                  首先,在 User 模型中创建另一个关系,即 必须是 hasOne 以获取最新活动:

                  First, create another relationship in User model, which has to be hasOne to get the latest activity:

                  public function latestActivity()
                  {
                      return $this->hasOne(Activity::class)->latest();
                  }
                  

                  然后只加载最近活动超过 3 天的用户

                  Then load just users who have latest activities older than 3 days

                  $users = User::whereHas('activites', function($q) {
                          $q->where('created_at', '<=', now()->subDays(3));
                      })
                      ->whereDoesntHave('activites', function($q) {
                          $q->where('created_at', '>', now()->subDays(3));
                      })
                      ->with('latestActivity')
                      ->get();
                  

                  这篇关于Laravel - 使用 whereHas 获取最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel - 根据动态参数扩展 Eloquent where 子句 下一篇:Laravel 在数据透视表中雄辩的 UUID

                  相关文章

                  最新文章

                  <small id='8EyFx'></small><noframes id='8EyFx'>

                    <bdo id='8EyFx'></bdo><ul id='8EyFx'></ul>

                    1. <i id='8EyFx'><tr id='8EyFx'><dt id='8EyFx'><q id='8EyFx'><span id='8EyFx'><b id='8EyFx'><form id='8EyFx'><ins id='8EyFx'></ins><ul id='8EyFx'></ul><sub id='8EyFx'></sub></form><legend id='8EyFx'></legend><bdo id='8EyFx'><pre id='8EyFx'><center id='8EyFx'></center></pre></bdo></b><th id='8EyFx'></th></span></q></dt></tr></i><div id='8EyFx'><tfoot id='8EyFx'></tfoot><dl id='8EyFx'><fieldset id='8EyFx'></fieldset></dl></div>
                      <tfoot id='8EyFx'></tfoot>

                    2. <legend id='8EyFx'><style id='8EyFx'><dir id='8EyFx'><q id='8EyFx'></q></dir></style></legend>