我在使Laravel为测试用例加载正确的.env文件时遇到一些问题。我正在使用PHPUnit,并在phpunit.xml中设置了以下变量:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
<exclude>
<file>./app/Http/routes.php</file>
</exclude>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
在我的.env.testing文件中,我有:
APP_ENV=testing
DB_CONNECTION=sqlite
Run Code Online (Sandbox Code Playgroud)
我在config / database.php下建立了他的连接:
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
]
Run Code Online (Sandbox Code Playgroud)
它只是没有加载.env.testing文件。如果我在TestCase.php中这样做:
dd(env('APP_ENV'));
Run Code Online (Sandbox Code Playgroud)
我仍然从我的.env文件中获得“发展”
我也尝试过使用:
$app->loadEnvironmentFrom('.env.testing');
Run Code Online (Sandbox Code Playgroud)
就像这里的线程中建议的那样
有谁知道可能出什么问题?
我有一个关于在Laravel中保存多态关系的问题.这是我想在laravel中创建的模型.
商店有许多产品,产品可以是"项目","活动"或"服务".
我有以下表格:
这就是我设置模型的方式:
class Shop extends Model{
public function products(){
return $this->hasMany('App\Product');
}
}
class Product extends Model{
public function productable(){
return $this->morphTo();
}
}
class Event extends Model{
public function product(){
return $this->morphOne('App\Product','productable');
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做到以下几点:
$shop = Shop::first()
$event = Event::create(['title'=>'Some event']);
$service = Service::create(['title' => 'Some service']);
$shop->products()->save($event);
$shop->products()->save($service);
Run Code Online (Sandbox Code Playgroud)
但它不起作用!当我试图保存我得到的关系时:
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1 no such column: shop_id (SQL: …Run Code Online (Sandbox Code Playgroud) 我正试图通过邮递员在Laravel中通过REST api上传视频(我也尝试了图像).
到目前为止,我已尝试进行最简单的上传,但似乎没有任何效果.我正在使用PostMan中的"form-data"选项对http:// localhost:8000/api/videos进行POST请求并设置"文件"选项,这样我就可以使用"浏览"按钮查找文件了我的电脑并调用文件"文件".
在控制器中我这样做:
return $request->file('file')->getClientOriginalExtension();
Run Code Online (Sandbox Code Playgroud)
但我得到错误"在null上调用成员函数getClientOriginalExtension()".我也尝试过:
return Input::file('file')->getClientOriginalExtension();
Run Code Online (Sandbox Code Playgroud)
但结果却是一样的.
任何人都知道我做错了什么?
我正在尝试在 ElasticSearch 中编写一个查询,我将 multi_match 与过滤器结合使用,以获取 id 或数字 og id。
这是我到目前为止:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "Kasper",
"fields": ["name", "first_name", "last_name"]
}
},
"filter": {
"term": {
"user_id": "ea7528f0-1b8a-11e8-a492-13e39bbd17cb"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
查询的“必须”部分完美运行,当我单独运行它时,我得到两个结果。当我从两个结果之一中挑选出“user_id”并使用该 id 添加查询的“过滤器”部分时,我什么也没得到。
我真正想做的是在 SQL 中使用类似的东西,其中 user_id in ('id1', 'id2'),因此过滤将类似于:
...,
"filter": {
"terms": {
"user_id": ["ea7528f0-1b8a-11e8-a492-13e39bbd17cb"]
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里误解了什么吗?
我想创建一个带有拦截器的全局 axios 实例,它添加一个带有访问令牌的标头。访问令牌存储在 cookie 中,但我不知道如何访问拦截器内的 cookie,因为它不是反应组件。
我尝试在这样的自定义钩子中创建 axios 实例:
import React, { useEffect, useState } from "react";
import { useCookies } from "react-cookie";
import axios from "axios";
function useApiClient() {
const [cookies, setCookie] = useCookies(["token"]);
const [client, setClient] = useState(null);
useEffect(() => {
setClient(
axios
.create({
baseURL: "http://localhost:8080/api/",
responseType: "json",
})
.interceptors.request.use(
(config) => {
console.log("setting up");
if (cookies["token"] != null) {
config.headers.authorization = cookies["token"];
}
},
(error) => Promise.reject(error)
)
);
}, [client]);
return client;
}
export default useApiClient; …Run Code Online (Sandbox Code Playgroud)