Php Laravel、Update查询和FIRSTORCEATE方法

Php Laravel、Update查询和FIRSTORCEATE方法,php,laravel,Php,Laravel,我有更新查询,所以我至少有4个(where和increment内容),这是重复的,需要花时间在一个on上搜索 我的问题:这样做有什么捷径吗 DB::table('New_Themes') ->where('id', $id) ->decrement('upVotes', 1); DB::table('New_Themes') ->where('i

我有更新查询,所以我至少有4个(where和increment内容),这是重复的,需要花时间在一个on上搜索

我的问题:这样做有什么捷径吗

   DB::table('New_Themes')
                ->where('id', $id)
                ->decrement('upVotes', 1);
                DB::table('New_Themes')
                ->where('id', $id)
                ->increment('downVotes', 1);
...
但它不起作用,你知道如何使它更简单吗

第二个问题:如何在Laravel中使用first或create方法,我需要此功能来简化代码。但我不知道如何将它与模型、控制器结合起来

DB::table('New_Themes')
                ->where('id', $id)
                ->decrement('upVotes', 1);
                ->increment('downVotes', 1);
什么样的变量是$flight、boolean或…?,任何解释都会对我很有帮助,因为laravel文档缺少详细信息

一,。 如果你不止一次使用它,你就不必输入它

2. $flight是model flight的一个实例,您应该学习如何使用model,这将使您在处理数据时的生活更加轻松,并充分利用MVC体系结构Laravels doc对此有很好的记录。

1。
function downVote($id)
{
      DB::table('New_Themes')
          ->where('id', $id)
          ->decrement('upVotes', 1)
          ->increment('downVotes', 1);
}
如果你不止一次使用它,你就不必输入它

2. $flight是model flight的一个实例,您应该学习如何使用model,这将使您在处理数据时的生活更加轻松,并充分利用MVC体系结构Laravels doc对此有很好的文档记录

function downVote($id)
{
      DB::table('New_Themes')
          ->where('id', $id)
          ->decrement('upVotes', 1)
          ->increment('downVotes', 1);
}
您可以看到文件
vendor/laravel/framework/src/illumb/Database/Eloquent/Model.php


您可以看到文件
vendor/laravel/framework/src/illumb/Database/elount/Model.php

它将是创建/更新元素的对象。它将是创建/更新元素的对象。它说我:语法错误,意外的'->'(T_object_操作符),为什么会发生这种情况?当我在->增量('downvows',1)下面添加时会发生这种情况;我发现这个finnaly DB::table('New_Themes')->where('id',$id)->update(['upvows'=>DB::raw('upvows-1'),'downvows'=>DB::raw('downvows+1'),]);工作得很好它告诉我:语法错误,意外的'->'(T_OBJECT_操作符),为什么会发生这种情况?当我在->增量('downvoates',1)下面添加时会发生这种情况;我发现这个finnaly DB::table('New_Themes')->where('id',$id)->update(['upvows'=>DB::raw('upvows-1'),'downvows'=>DB::raw('downvows+1'),]);效果很好
/**
     * Get the first record matching the attributes or create it.
     *
     * @param  array  $attributes
     * @return static
     */
    public static function firstOrCreate(array $attributes)
    {
        if ( ! is_null($instance = static::where($attributes)->first()))
        {
            return $instance;
        }

        return static::create($attributes);
    }