【发布时间】:2019-07-12 07:56:02
【问题描述】:
我正在创建 laravel/vue.js CRUD 应用程序,目前一切正常,但我担心更新数据后对数据库的查询质量。
每次更新数据库中的行时,我都在使用 getAllData()。现在,当我在数据库中有几条记录时,每次询问服务器并在 vue 中呈现新列表不是问题,但是当我有几千行时,它会使我的应用程序变得缓慢而沉重。
现在我像这样更新数据库:
这是 我的 vue.js 更新功能的一部分:
updateStatus: function(id){
var index = _.findIndex(this.rows,["id",id]);
if (this.rows[index].pay_status=="waiting"){
axios.put("http://127.0.0.1:8000/api/payments/"+id
,{pay_status:"payed"}).then((response)=>{
this.getAllData();
}
这是我的 vue.js getAllData 函数:
getAllData: function(){
axios.get("http://127.0.0.1:8000/api/payments").then((response)=>{
this.rows = response.data;
});
}
还有我的PaymentsController:
namespace App\Http\Controllers;
use App\Payments;
use App\Suppliers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Faker\Generator;
class PaymentsController extends Controller
{
public function index()
{
$payments = Payments::with('suppliers')->get();
return response($payments, Response::HTTP_OK);
}
}
我的更新功能:
public function update(Request $request, $id)
{
$payments = new Payments();
payments::where('id', $id)->update($request->all());
}
是否有任何方法可以更有效地进行更新,例如仅从数据库中获取更新的行并将其放入现有的行对象中?或者我不应该担心?
【问题讨论】:
-
向我们展示来自控制器的
api/payments/{id}路由的逻辑
标签: sql database laravel vue.js