我有一个phone_models,phone_problems和一个phone_model_phone_problem数据透视表.数据透视表有一个额外的列"价格".
PhoneModel:
class PhoneModel extends \Eloquent
{
public function problems()
{
return $this->belongsToMany('RL\Phones\Entities\PhoneProblem')->withPivot('price');
}
}
Run Code Online (Sandbox Code Playgroud)
PhoneProblem:
class PhoneProblem extends \Eloquent
{
public function models()
{
return $this->belongsToMany('PhoneModel')->withPivot('price');
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是获得具有特定问题的特定手机的价格.
这就是我现在的方式,但我觉得Laravel有一个内置的Eloquent功能,我找不到这样做的方式更简单:
$model = $this->phoneService->getModelFromSlug($model_slug);
$problem = $this->phoneService->getProblemFromSlug($problem_slug);
Run Code Online (Sandbox Code Playgroud)
所有这一切都是从他们的slug中选择特定的模型和问题.
那么我所做的就是凭借这些凭据我得到的价格是这样的:
$row = DB::table('phone_model_phone_problem')
->where('phone_model_id', '=', $model->id)
->where('phone_problem', '=', $problem->id)
->first();
Run Code Online (Sandbox Code Playgroud)
所以现在我可以得到这样的价格,$row->price但我觉得需要一个更简单,更"Laravel"的方式来做到这一点.
我不确定这是一个错误还是http响应包应该如何工作.
在此示例中,Content-Type不会设置响应标头
// Return the response
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
Run Code Online (Sandbox Code Playgroud)
但是,如果我按顺序设置标题的顺序,它确实有效:
// Return the response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(js)
Run Code Online (Sandbox Code Playgroud)
现在这将实际设置标头application/json.这种行为是有意的吗?
我有一个预先存在的golang项目,具有以下文件夹结构(最小化文件夹以便于阅读).
- postgre
- service.go
- cmd
- vano
- main.go
- vanoctl
- main.go
vano.go
Run Code Online (Sandbox Code Playgroud)
现在,因为我的项目Web服务器在./cmd/vano我需要创建一个自定义Buildfile和Procfile.所以我做到了
这是我的Buildfile
make: ./build.sh
Run Code Online (Sandbox Code Playgroud)
build.sh文件:
#!/usr/bin/env bash
# Install dependencies.
go get ./...
# Build app
go build ./cmd/vano -o bin/application
Run Code Online (Sandbox Code Playgroud)
最后我的Procfile:
web: bin/application
Run Code Online (Sandbox Code Playgroud)
所以现在我的文件夹结构如下所示:
- postgre
- service.go
- cmd
- vano
- main.go
- vanoctl
- main.go
vano.go
Buildfile
build.sh
Procfile
Run Code Online (Sandbox Code Playgroud)
我使用git压缩源代码:
git archive --format=zip HEAD > vano.zip
Run Code Online (Sandbox Code Playgroud)
并将其上传到AWS Beanstalk.我如何不断收到错误,AWS错误似乎并不是最常见的.这是我的错误
Command execution completed on all instances. …Run Code Online (Sandbox Code Playgroud) 所以我正在查看net/proxy文档,并且没有任何示例可以使用它的任何方法.我正在研究使用socks5.这是函数的外观:
func SOCKS5(network, addr string, auth *Auth, forward Dialer) (Dialer, error)
Run Code Online (Sandbox Code Playgroud)
现在一切都有意义,除非我对forward哪个类型Dialer和函数本身返回一个感到困惑Dialer.其他任何有意义的事情就是让network, add, auth我失望.如何设置我的客户端以使用socks5拨号器?
我很难将字符串分配给类型字符串.所以我有这种类型:
type Username string
Run Code Online (Sandbox Code Playgroud)
然后我有一个返回字符串的函数
我要做的是将用户名设置为返回的字符串:
u := &Username{returnedString}
Run Code Online (Sandbox Code Playgroud)
我也试过了
var u Username
u = returnedString
Run Code Online (Sandbox Code Playgroud)
但我总是得到一个错误.
我execSync用来运行soffice命令。我遇到的问题是引发错误时,execSync只是将错误记录到控制台,无法捕获它。我尝试使用一条try catch语句,但它仍然只是将错误记录到控制台。
function convertToPdf(filepath, destpath) {
var cmd = 'sofice command';
try {
var res = execSync(cmd, { encoding: 'utf8' });
} catch (e) {
console.log("Errors:", e);
}
console.log("res:", res);
}
convertToPdf("test.docx");
Run Code Online (Sandbox Code Playgroud)
我运行并返回:
Error: source file could not be loaded
res:
Run Code Online (Sandbox Code Playgroud)
请注意,即使明显抛出错误,我的catch语句也永远不会被记录,但是Error:由于我没有记录错误,因此会自动记录另一条消息。
所以我有一个在 Heroku 上运行的 go rest api。我有免费套餐版本,但现在我们已将该应用程序转移到生产环境并升级到 standard-0 数据库。我的代码没有改变,唯一改变的是连接到数据库的环境变量。但是,当我尝试连接到数据库时,出现以下错误:
[BRONZE] [5-1] sql_error_code = 28000 FATAL: no pg_hba.conf entry for host "54.234.***.***", user "ub21ndj3*****", database "d8useg2o3****", SSL off
Run Code Online (Sandbox Code Playgroud)
据我了解,这是一个 SSL 问题。我不明白的是我的应用程序正在 Heroku 服务器上运行,因此我不需要创建 tls 配置来连接到数据库,并且相同的代码可以与以前的数据库一起使用。是否还有其他问题导致我收到此错误?
所以我经常遇到这个问题go。假设我有一个包含 100,000 行文本的文本文件。现在我想将所有这些行保存到数据库中。所以我会做这样的事情:
file, _ := iotuil.ReadFile("file.txt")
fileLines := strings.Split(string(file), "\n")
Run Code Online (Sandbox Code Playgroud)
现在我将遍历文件中的所有行:
for _, l := range fileLines{
saveToDB(l)
}
Run Code Online (Sandbox Code Playgroud)
现在我想saveToDB同时运行这个函数:
var wg sync.WaitGroup
for _, l := range fileLines{
wg.Add(1)
go saveToDB(l, &wg)
}
wg.Wait()
Run Code Online (Sandbox Code Playgroud)
我不知道这是否有问题,但那会运行 100,000 个并发函数。有什么办法可以说嘿,运行 100 个并发函数,等待所有这些函数完成,然后再运行 100 个。
for i, _ := range fileLine {
for t = 0; t < 100; t++{
wg.Add(1)
go saveToDB(fileLine[i], &wg)
}
wg.Wait()
}
Run Code Online (Sandbox Code Playgroud)
我需要做类似的事情还是有更干净的方法来解决这个问题?或者我运行 100,000 个并发任务不是问题?
我正在尝试向我现有的表之一添加 EXCLUDE 约束,这是我正在运行的 sql:
ALTER TABLE appointment_service
ADD COLUMN start_time bigint,
ADD COLUMN end_time bigint,
EXCLUDE USING gist (
professional_id WITH =,
int8range(start_time, end_time) WITH &&
),
ADD COLUMN professional_id text REFERENCES professionals ON DELETE CASCADE ON UPDATE CASCADE
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误。
ERROR: syntax error at or near "EXCLUDE"
LINE 4: EXCLUDE USING gist (
Run Code Online (Sandbox Code Playgroud)
完成此操作的正确 SQL 语法是什么?
我正在使用带有babel的webpack来编译我的ecmascript 6代码.一切正常,但如果我添加某些dependeciens喜欢请求npm包.这是我的文件:
main.js
import os from 'os'
export class User {
constructor(username) {
this.username = username;
}
register() {
console.log("registering...");
}
}
var client = new User("hey")
console.log(user.register());
Run Code Online (Sandbox Code Playgroud)
webpack配置:
var webpack = require('webpack')
module.exports = {
entry: [
'./src/main.js'
],
output: {
path: "dist",
publicPath: "/dist/",
filename: "stela.js"
},
watch: false,
module: {
loaders: [{
test: /\.js$/,
// excluding some local linked packages.
// for normal use cases only node_modules is needed.
exclude: /node_modules/,
loader: 'babel'
}, {
test: /\.json$/, …Run Code Online (Sandbox Code Playgroud) go ×6
javascript ×2
node.js ×2
postgresql ×2
concurrency ×1
exec ×1
heroku ×1
laravel ×1
many-to-many ×1
mysql ×1
php ×1
pivot-table ×1
socks ×1
sql ×1
types ×1
webpack ×1