Php 通过laravel中的id获取所有用户的电子邮件

Php 通过laravel中的id获取所有用户的电子邮件,php,laravel,Php,Laravel,我想向第二个表中有激活选项但不确定如何激活的用户发送电子邮件 逻辑 从邮件表中获取所有用户id 检查他们的latest_blog列是否设置为on 向这些用户发送电子邮件 代码 dd($latest_blog)返回 Collection {#1705 ▼ #items: array:5 [▼ 0 => 41 1 => 51 2 => 42 3 => 60 4 => 61 ] } dd($users)仅返回1个用户,而所

我想向第二个表中有激活选项但不确定如何激活的用户发送电子邮件

逻辑
  • 邮件
    表中获取所有用户id
  • 检查他们的
    latest_blog
    列是否设置为
    on
  • 向这些用户发送电子邮件
  • 代码
    dd($latest_blog)返回

    Collection {#1705 ▼
      #items: array:5 [▼
        0 => 41
        1 => 51
        2 => 42
        3 => 60
        4 => 61
      ]
    }
    
    dd($users)
    仅返回1个用户,而所有用户的
    最新日志
    列设置为
    on
    。所以基本上它应该返回5个用户,而不是1个

    Collection {#1758 ▼
      #items: array:1 [▼
        0 => User {#1756 ▶}
      ]
    }
    
    有什么想法吗

    更新
    邮寄模式

    protected $fillable = [
      'user_id', 'interests', 'security', 'latest_projects', 'latest_blog'
    ];
    
    public function user()
    {
      return $this->belongsTo(User::class);
    }
    
    protected $fillable = [
      'name', 'username', 'email', 'password', 'points', 'google2fa_secret'
    ];
    
    public function mails()
    {
      return $this->hasOne(Mailing::class);
    }
    
    用户模型

    protected $fillable = [
      'user_id', 'interests', 'security', 'latest_projects', 'latest_blog'
    ];
    
    public function user()
    {
      return $this->belongsTo(User::class);
    }
    
    protected $fillable = [
      'name', 'username', 'email', 'password', 'points', 'google2fa_secret'
    ];
    
    public function mails()
    {
      return $this->hasOne(Mailing::class);
    }
    
    邮件模式

    Schema::create('mailings', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('user_id')->unsigned()->nullable()->unique();
                $table->string('interests')->default('on');
                $table->string('security')->default('on');
                $table->string('latest_projects')->default('on');
                $table->string('latest_blog')->default('on');
                $table->timestamps();
            });
            Schema::table('mailings', function (Blueprint $table) {
             $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
    
    这将有助于:

    $latest_blog = Mailing::where('latest_blog', 'on')->get()->pluck('user_id');
    // dd($latest_blog);
    $users = User::whereIn('id', $latest_blog->toArray())->get();
    foreach($users as $user){
      Mail::to($user->email)->send(new BlogUpdates($user, $post));
    }
    

    您所做的一切都是正确的,但您正在
    where
    子句中添加额外的数组。由于Pull已经返回了一个数组,因此不需要在
    中再次添加[],其中
    子句so

    你的代码应该是

    $latest_blog = Mailing::where('latest_blog', 'on')->pluck('user_id');
    $users = User::whereIn('id', $latest_blog)->get();
    foreach($users as $user){
       Mail::to($user->email)->send(new BlogUpdates($user, $post));
    }
    

    希望您能理解。

    请向我们展示两个表的表结构和示例数据。我的一般建议是:让原始的MySQL查询完全工作,然后担心如何在Laravel中编码。您将
    dd($users)
    放在哪里?如果您已在循环内转储,则它将始终仅显示用户。@SagarGautam否它位于foreach上方的
    $users
    @TimBiegeleisen下,我将更新我的question@TimBiegeleisenupdated.pluck已经返回数组,那么为什么我们需要数组中的
    ->toArray()
    函数呢?@SagarGautam“虽然大多数雄辩的集合方法返回雄辩集合的新实例,但Pull、keys、zip、collapse、flatten和flip方法返回基本集合实例。同样,如果映射操作返回的集合不包含任何雄辩的模型,它将自动转换为基本集合。“@MostafaHadian这很好,但我的观点是,当我们使用pull时,它将返回关联的或索引的数组,这对于雄辩的
    where
    子句来说已经是可以接受的。因此,我认为我们不需要额外的
    来排列
    @maforti,因为我认为您以前只获得了代码中的第一个用户,现在您将获得正确的用户。无论如何,恭喜你。