php - 优化 AJAX - Laravel 功能
问题描述
我在一些公式上有点挣扎,这个想法是对所有东西都只有一个公式/函数,所以它很容易维护并且很健壮。
问题是试图结合 AJAX 调用和 laravel 函数。
从一方面来说,我有一个 AJAX 数据表控制器(调用需要采用这种格式):
public function userData(Request $request)
{
$event = User::select(
'users.*',
DB::raw('IFNULL(b.balance,0) as balance'),
)
->leftJoin(DB::raw('(SELECT seller_id, SUM(total) as balance FROM transactions WHERE concept IN ("TPV") AND status = "ok" GROUP by buyer_id)as b'), 'b.seller_id', '=', 'users.id')
->get();
return $this->formatView($request, $event, 'user');
}
然后,我用于网络其余部分的公式位于模型内的函数中:
public function Balance($seller_id = false){
return Transaction::emitted()
->where('event_id', $this->id)
->where('seller_id', $this->seller_id)
->whereIn('concept', ['TPV'])
->where('status', 'ok')
->sum('total');
}
问题是:你知道如何只使用一个公式/函数来处理所有事情吗?
解决方案
在你的控制器方法中试试这个
在单独的方法中进行常用计算并在此处调用。然后在以下部分更改响应格式
if ($request->expectsJson()){
//send response to ajax here in json format. note that you should set ajax dataType:'json'
}
//send response for web here.
推荐阅读
- python-3.7 - 无法通过 pipenv 安装 ccxt.pro - pipenv 需要 #egg 片段来控制版本控制依赖项
- visual-studio-code - Visual Studio Code - 使 Intellisense 完成其他文件中的关键字
- spring-boot - Spring Boot Web 客户端 XML
- javascript - 在 Rails 6 上未定义 Instantclick
- nginx - 使用 kubernetes 秘密使 nginx 可配置?
- forms - Sysmfony 4.4 - TimeType 选项中带有“约束”的测试表单生成 UndefinedOptionsException
- java - Api 21 上的工具栏项目 MaterialButton 中的颜色错误
- python-3.x - 使用 python 3.x 解析 nmap 结果字符串
- java - 根据本地 XML 文件验证 XML 文件的最佳方法是什么?
- python - 也许我在 pytorch 上发现了一些奇怪的东西,导致属性设置器不起作用