DB::table('New_Themes')
->where('id', $id)
->decrement('upVotes', 1);
DB::table('New_Themes')
->where('id', $id)
->increment('downVotes', 1);
...
我有更新查询,所以我至少有4个(where和increment的东西(,这是重复的,在on上搜索需要时间。
我的问题:
DB::table('New_Themes')
->where('id', $id)
->decrement('upVotes', 1);
->increment('downVotes', 1);
但它不起作用,你知道如何让它更简单吗?
第二个问题:如何在Laravel中使用firstOrCreate方法,我需要这个功能来简化代码。但我不知道如何将它与模型、控制器集成。
Ex. $flight = AppFlight::firstOrCreate(['name' => 'Flight 10']); (from documentation)
$flight是什么样的变量,布尔值还是。。。?,任何解释对我都很有帮助,因为laravel文档缺乏详细信息。
1
function downVote($id)
{
DB::table('New_Themes')
->where('id', $id)
->decrement('upVotes', 1)
->increment('downVotes', 1);
}
如果你不止一次使用它,你就不必键入它。
2
$flight是模型flight的一个例子,你应该学习如何使用模型,这将使你在处理数据时的生活更轻松,并充分使用MVC架构。Laravels文档对此有很好的记录。
/**
* 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);
}
您可以看到文件vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php