从表中选择,其中两列具有相同的值

时间:2018-01-30 08:20:02

标签: database laravel eloquent

我有选择所有文章的代码。我认为,我所做的是错误的。我正在选择所有值,然后我正在执行它们。

Article::all()->each(function($article) {
         if ($article->created_at == $article->published_at && $article>published) {
             $article->update(['published_at' => null]);
         } 
     });

我需要做的只是选择那些提到的created_at和updated_at相同的文章 我怎么能在数据库级别上这样做? 我需要那样:

Article::where('created_at', '=', 'published_at')->where('published')->get()->each(function($article) {
           $article->update(['published_at' => null]);
     }
});

这段代码肯定不起作用,只是为了想象。

2 个答案:

答案 0 :(得分:2)

编辑您的代码

Article::all()->each(function($article) {
     if ($article->created_at == $article->published_at && $article->published) {
         $article->update(['published_at' => null]);
     } 
 });

答案 1 :(得分:1)

您只需在两列中搜索一个值:

Article::where(['created_at' => $date, 'updated_at' => $date ])->get();

编辑:

将whereRaw用于此

Article::whereRaw('created_at = updated_at')->get();
相关问题