我有一个问题表和一个标签表.我想从给定问题的标签中获取所有问题.因此,举例来说,我可能会在给定问题上附加标签"旅行","火车"和"文化".我希望能够获取这三个标签的所有问题.看起来棘手的是,问题和标签关系是Eloquent中定义为manyToMany的多对多关系.
我想过尝试合并问题集合如下:
foreach ($question->tags as $tag) {
if (!isset($related)) {
$related = $tag->questions;
} else {
$related->merge($tag->questions);
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用.似乎没有合并任何东西.我正确地尝试了吗?另外,在Eloquent中,是否有更好的方法来获取多对多关系中的一行行?
PHP的内置服务器不使用.htaccess吗?有道理我认为因为它不依赖于Apache(?).无论如何,有可能告诉服务器使用这些文件,它能处理URL重写吗?我在框架中有一些依赖于这些文件的项目.
APPLICATION_ENV=development php -S localhost:8000 -t public/
我注意到Laravel有一个简洁的方法Request::wantsJson- 我假设当我发出请求时我可以传递信息来请求JSON响应,但是我该怎么做,以及Laravel使用什么标准来检测请求是否要求JSON?
我正在管理一个Google帐户,它有一个与Google+个人资料相关联的YouTube频道,以及一个与Google+信息页相关联的YouTube频道.使用此帐户的OAuth密钥,我想将视频上传到"页面"的频道,到目前为止我只使用"个人资料"的频道进行管理."页面"频道有我们公司的名称和标识,所以我不希望它是个人(至少看起来像)"个人资料"的频道
我使用的脚本几乎与视频上传示例脚本相同:
https://developers.google.com/youtube/v3/code_samples/php?hl=en#upload_a_video
<?php
// Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
/*
* You can acquire an OAuth 2.0 client ID and client secret from the
* Google Developers Console <https://console.developers.google.com/>
* For more information about using OAuth 2.0 to access Google APIs, please see:
* <https://developers.google.com/youtube/v3/guides/authentication>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
$client = new …Run Code Online (Sandbox Code Playgroud) 我的项目中有以下结构:
/
/app
/app/models/ --UserTable.php
/lib
/lib/framework
/lib/framework/Models
/lib/framework/Db
/tests -- phpunit.xml, bootstrap.php
/tests/app
/tests/app/models --UserTableTest.php
Run Code Online (Sandbox Code Playgroud)
使用app和lib目录,我有各种类可以一起运行我的应用程序.要设置我的测试,我创建了一个/tests/phpunit.xml文件和/tests/bootstrap.php
phpunit.xml
<phpunit bootstrap="bootstrap.php">
</phpunit>
Run Code Online (Sandbox Code Playgroud)
bootstrap.php中
<?php
function class_auto_loader($className)
{
$parts = explode('\\', $className);
$path = '/var/www/phpdev/' . implode('/', $parts) . '.php';
require_once $path;
}
spl_autoload_register('class_auto_loader');
Run Code Online (Sandbox Code Playgroud)
所以我有以下测试:
<?php
class UserTableTest extends PHPUnit_Framework_TestCase
{
protected $_userTable;
public function setup()
{
$this->_userTable = new app\models\UserTable;
}
public function testFindRowByPrimaryKey()
{
$user = $this->_userTable->find(1);
$this->assertEquals($user->id, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我进行测试时它找不到课程 - PHP Fatal error: Class 'app\models\UserTable' not found …
我正在做以下事情
public function boot(DispatcherContract $events)
{
parent::boot($events);
// set Tag slug
Tag::saving(function($tag)
{
//slugify name
$tag->slug = Str::slug($tag->name);
});
}
Run Code Online (Sandbox Code Playgroud)
当我在修补程序中运行它时,我收到以下错误:
PHP Fatal error: Class 'App\Providers\Str' not found in /var/www/questions-l5/app/Providers/EventServiceProvider.php on line 35
Run Code Online (Sandbox Code Playgroud)
..但我不知道Laravel导入它的方式.我是否需要使用use,我尝试将以下内容添加到config/app.php文件中:
'aliases' => [
...
'Str' => 'Illuminate\Support\Str',
Run Code Online (Sandbox Code Playgroud)
..但似乎没有太大的区别.
http://chrishayes.ca/blog/code/laravel-4-generating-unique-slugs-elegantly http://laravel.com/api/5.0/Illuminate/Support/Str.html
我一直在读Slim v2,$ app被绑定到中间件类.我发现v3不是这样的吗?下面是我的中间件类,但我只是未定义:
<?php
namespace CrSrc\Middleware;
class Auth
{
/**
* Example middleware invokable class
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke($request, $response, $next)
{
// before
var_dump($this->getContainer()); // method undefined
var_dump($this->auth); exit; // method undefined
if (! $this->get('auth')->isAuthenticated()) {
// Not authenticated and must be authenticated to access this resource
return $response->withStatus(401);
}
// pass onto the next callable …Run Code Online (Sandbox Code Playgroud) 我正在做以下测试对Laravel的POST调用.我希望根据我的路线,问题的POST将作为商店操作方法发送.这适用于浏览器.
我的测试:
public function setUp()
{
parent::setUp();
Session::start();
}
public function testStoreAction()
{
$response = $this->call('POST', 'questions', array(
'_token' => csrf_token(),
));
$this->assertRedirectedTo('questions');
}
Run Code Online (Sandbox Code Playgroud)
但是,我告诉我重定向不匹配.此外,我可以看到它根本不是商店行动方法.我想知道它将采用什么动作方法,以及它为什么不去存储方法(如果我查看路径:列表我可以看到有一个POST问题/路由应该转到questions.store;这个也适用于浏览器,但不适用于我的测试).此外,我正在为此资源正确编写呼叫吗?我在这里添加了令牌,因为它正在抛出异常,在某些测试中我会让令牌检查通过.
我正在尝试做一些我以前从未做过的事情:克隆另一个用户(codeforamerica)存储库并在我的计算机上本地运行它,目的是对它进行自己的更改.
我已经设法将它分叉到我自己的存储库,并克隆它:
git clone https://github.com/martynbiz/human_services_finder.git
Run Code Online (Sandbox Code Playgroud)
...但是当我直接在框中执行以下操作时:
cd human_services_finder
rails s
Run Code Online (Sandbox Code Playgroud)
......它告诉我:
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
Run Code Online (Sandbox Code Playgroud)
...但是,如果我进入我自己的应用程序并运行rails s它运行服务器确定.有什么东西丢失我需要运行这个作为Rails应用程序吗?对不起,这个有点初学者.谢谢
我正在尝试生成聚合结果但重命名一些字段:
db.articles.aggregate([
{$match: {
"model.lang": "en"
}},
{$project: {
"_id": 0,
"model.title": 1,
"model.address_en": "$address",
"model.date": { $dateToString: { format: "%Y-%m-%d", date: "$date" } }
}}
]);
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我正在尝试将"model.title"重命名为"title",将"model.address_en"重命名为"address",将"model.date"重命名为"date"..但没有太多运气:
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null } }
{ "model" : { "date" : null …Run Code Online (Sandbox Code Playgroud)