我已经成功地将Eloquent用作Slim Framework 2中的独立包.
但现在我想使用Illuminate\Support\Facades\DB,因为我需要通过从2个表中获取信息并使用数据库中的Left Join和Counter来显示一些统计信息,如下所示:
use Illuminate\Support\Facades\DB;
$projectsbyarea = DB::table('projects AS p')
->select(DB::raw('DISTINCT a.area, COUNT(a.area) AS Quantity'))
->leftJoin('areas AS a','p.area_id','=','a.id')
->where('p.status','in_process')
->where('a.area','<>','NULL')
->orderBy('p.area_id');
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Type: RuntimeException
Message: A facade root has not been set.
File: ...\vendor\illuminate\support\Facades\Facade.php
Line: 206
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
到目前为止,我已经发现,在此链接中我需要创建一个新的应用程序容器,然后将其绑定到Facade.但我还没有找到如何让它发挥作用.
这就是我开始其余的Eloquent和工作正常的方式:
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule();
$capsule->addConnection([
'my' => $app->config->get('settings'),
/* more settings ...*/
]);
/*booting Eloquent*/
$capsule->bootEloquent();
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
固定
为@ user5972059说,我不得不在$capsule->setAsGlobal();//This is important to make work the DB (Capsule)
上面添加$capsule->bootEloquent();
然后,查询执行如下:
use …
Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
Class 'App\Http\Controllers\File' not found
Run Code Online (Sandbox Code Playgroud)
当在laravel 5控制器中使用时:
$files = File::files( $this->csvDir );
Run Code Online (Sandbox Code Playgroud)
我必须将文件系统添加到composer.json
和config/app.php
.不知怎的,我使用了错误的配置.
这就是我改变的:
"require": {
"laravel/framework": "5.0.*",
"illuminate/html": "5.*",
"illuminate/filesystem": "5.*" /* my try to repo */
},
Run Code Online (Sandbox Code Playgroud)
'providers' => [
// [...]
// jerik 2015-04-17: get html Forms
// http://laravel.io/forum/09-20-2014-html-form-class-not-found-in-laravel-5
'Illuminate\Html\HtmlServiceProvider',
'Illuminate\Filesystem\FilesystemServiceProvider', // try to add file
Run Code Online (Sandbox Code Playgroud)
当我运行时composer update
,它运行良好,但没有输出文件系统的输出.所以我的配置错了,但我不知道正确的方法.
有什么建议或提示吗?
在我的独立(没有Laravel)项目中,我想使用Illuminate IoC容器.此外,我想App
通过illuminate/support
组件提供的外观访问应用程序容器.我安装了两个组件(v5.0.28).这是我的(简化)代码:
function setup_App(){
$container = new Illuminate\Container\Container();
Illuminate\Support\Facades\Facade::setFacadeApplication($container);
class_alias('Illuminate\Support\Facades\App', 'App');
}
setup_App();
App::bind('w', 'Widget');
$widget = App::make('w');
Run Code Online (Sandbox Code Playgroud)
不幸的是,尝试绑定某些内容会导致:
Fatal error: Call to undefined method Illuminate\Support\Facades\App::bind() in ...\illuminate\support\Facades\Facade.php on line 213
Run Code Online (Sandbox Code Playgroud)
这是该行的代码
$instance = static::getFacadeRoot();
...
return $instance->$method($args[0], $args[1]); // <--- line 213
Run Code Online (Sandbox Code Playgroud)
哪里$instance
是一个实例Illuminate\Support\Facades\App
,$method == 'bind'
,$args[0] == 'w'
和$args[1] == 'Widget'
.问题是它$instance
不是一个实例,Illuminate\Container\Container
并且类Illuminate\Support\Facades\App
没有任何支持在其静态属性上调用任意函数$app
.
为了使它工作,我将以下功能添加到Illuminate\Support\Facades\App
:
public function __call( $method , …
Run Code Online (Sandbox Code Playgroud) php ioc-container laravel illuminate-container laravel-facade
laravel 中的验证器可以自定义现有数据库规则,例如,如果您需要检查额外的列。手册中的一个例子:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::exists('staff')->where(function ($query) {
$query->where('account_id', 1);
}),
],
]);
Run Code Online (Sandbox Code Playgroud)
的query
在封闭不typehinted,所以我不是很积极,这是什么样的对象。我可以看到DatabaseRule
本身只具有的一些功能where
,wherenot
等等,但我期待添加一个加入进来。
给定的例子说电子邮件必须存在于具有 的员工account_id = 1
,但是如果团队(所有员工都是团队的一部分,这是一个单独的表)应该具有某个属性,例如team.active = 1
?
整个员工/团队的事情当然是一个例子
所以最后我想知道:我如何向这个规则添加一个连接,以便我们确保员工的团队有一个为 1 的“活动”列。
我的第一个问题可能是:那是什么类型的$query
?我想像这样的事情会很棒,但没有理由怀疑这是有效的:
Rule::exists('staff')->where(function ($query) {
$query
->join('team', 'team.team_id', '=', 'staff.team_id')
->where('team.active', 1);
})
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用。奇怪的是join本身并没有报错,而是好像被忽略了:
未找到列:
1054 “where 子句”中的未知列“team.active”
(SQL:从staff
whereteam
.active
= 1) 中选择 count(*) …
join laravel illuminate-container laravel-5 laravel-validation
我知道这里有几个类似的问题,但没有一个能解决我的问题.
我正在尝试在Ubuntu 14.04上添加带有Laravel 5的HtmlServiceProvider.我一直收到以下错误:
dl@dl-VirtualBox:~/l5todo$ composer update
> php artisan clear-compiled
PHP Fatal error: Class 'Illuminate\Html\HtmlServiceProvider' not found in /home/dl/l5todo/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 146
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Illuminate\Html\HtmlServiceProvider' not found
Script php artisan clear-compiled handling the pre-update-cmd event returned with an error
[RuntimeException]
Error Output: PHP Fatal error: Class 'Illuminate\Html\HtmlServiceProvider' not found i
n /home/dl/l5todo/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository
.php on line 146
Run Code Online (Sandbox Code Playgroud)
我的vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository
.php看起来像:
/**
* Create a new provider instance.
*
* @param string $provider
* @return \Illuminate\Support\ServiceProvider
*/
public function createProvider($provider)
{
return …
Run Code Online (Sandbox Code Playgroud) 作曲家更新并安装合同后,我收到此错误:
Fatal error: Declaration of Illuminate\Container\Container::make() must be compatible with Illuminate\Contracts\Container\Container::make($abstract, array $parameters = Array) in C:\xampp\htdocs\app\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 12
Run Code Online (Sandbox Code Playgroud)
找不到解决办法,请问有人知道如何解决吗?
我compiled.php
在vendor
文件夹中丢失了文件。所以当我把它拉回来时,一切都像以前一样。
好吧,现在我想登录或注册时得到这个:
类 App\User 包含 1 个抽象方法,因此必须声明为抽象方法或实现其余方法 (Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifierName)
contracts declaration composer-php illuminate-container laravel-5
我的 Laravel 有问题,我无法发布数据
一个错误说
InvalidArgumentException in Validator.php line 2593:
Validation rule unique requires at least 1 parameters.
Run Code Online (Sandbox Code Playgroud)
这是我的代码
public function postUbah(Request $request, $id)
$validator = Validator::make($request->all(), [
'username' => 'required|unique:user|min:5',
'name' => 'required',
'group' => 'required'
]);
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助。
这是我的模型
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'user';
protected $fillable = ['username', 'password'];
protected $hidden = ['password', 'remember_token'];
Run Code Online (Sandbox Code Playgroud) 简短的说明:我无法使用composer(https://packagist.org/packages/illuminate/container)安装Laravel容器进行方法注入.仅当在对象的构造函数中使用时,注入才有效.例如:
class SomeClass {
function __construct(InjectedClassWorksHere $obj) {}
function someFunction(InjectedClassFailsHere $obj) {}
}
Run Code Online (Sandbox Code Playgroud)
长话故事:我正在考虑重新考虑使用Laravel的一个重大项目,但由于业务压力,我无法投入我想要的时间.为了不让"宝宝带着洗澡水",我正在使用各个Laravel组件来提升旧分支中开发的代码的优雅.在评估Laravel时我最喜欢的一种新技术是依赖注入的概念.我很高兴后来发现我可以使用Laravel项目的外部.我现在有这个工作,一切都很好,除了在线发现的容器的开发版本似乎不支持方法注入.
有没有其他人能够让容器工作并在Laravel项目之外进行方法注入?
我的方法到目前为止......
composer.json
"illuminate/support": "5.0.*@dev",
"illuminate/container": "5.0.*@dev",
Run Code Online (Sandbox Code Playgroud)
应用程序引导代码:
use Illuminate\Container\Container;
$container = new Container();
$container->bind('app', self::$container); //not sure if this is necessary
$dispatcher = $container->make('MyCustomDispatcher');
$dispatcher->call('some URL params to find controller');
Run Code Online (Sandbox Code Playgroud)
有了上面的内容,我可以注入我的控制器的构造函数,但不能注入它们的方法方法.我错过了什么?
完整源代码...(C:\ workspace\LMS> php cmd\test_container.php)
<?php
// This sets up my include path and calls the composer autoloader
require_once "bare_init.php";
use Illuminate\Container\Container;
use Illuminate\Support\ClassLoader;
use Illuminate\Support\Facades\Facade;
// Get a reference to the root …
Run Code Online (Sandbox Code Playgroud) 当我想在laravel 5.2项目中添加表单时,在composer中遇到了一些错误。之后,我的整个项目出现了一个奇怪的错误:
致命错误:C:\ xampp \ htdocs \ gifkadeh \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.Container.php:738堆栈跟踪中未捕获的异常“ ReflectionException”,消息为“类App \ Http \ Kernel不存在” :#0 C:\ xampp \ htdocs \ gifkadeh \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php(738):ReflectionClass-> __ construct('App \ Http \ Kernel')#1 C:\ xampp \ htdocs \ gifkadeh \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php(633):Illuminate \ Container \ Container-> build('App \ …
鉴于我的班级
<?php
declare(strict_types=1);
use Illuminate\Support\Collection;
use stdClass;
class PhpstanIssue
{
/**
* @param Collection<Collection<stdClass>> $collection
*
* @return Collection<Foo>
*/
public function whyDoesThisFail(Collection $collection): Collection
{
return $collection
->flatten() // Collection<stdClass>
->map(static function (\stdClass $std): ?Foo {
return Foo::get($std);
}) // should now be Collection<?Foo>
->filter(); // should now be Collection<Foo>
}
}
Run Code Online (Sandbox Code Playgroud)
我非常困惑为什么 phpstan (0.12.64) 会失败:
18: [ERROR] Method PhpstanIssue::whyDoesThisFail() should return
Illuminate\Support\Collection&iterable<Foo> but returns
Illuminate\Support\Collection&iterable<Illuminate\Support\Collection&iterable<stdClass>>. (phpstan)
Run Code Online (Sandbox Code Playgroud)
为什么 phpstan 无法推断出该管道的正确结果类型?如何让 phpstan 理解管道?
我可以验证我的代码在 phpunit 测试用例中是否有效:
class MyCodeWorks extends TestCase …
Run Code Online (Sandbox Code Playgroud) laravel ×7
php ×7
laravel-5 ×5
composer-php ×1
contracts ×1
declaration ×1
eloquent ×1
join ×1
laravel-5.2 ×1
namespaces ×1
package ×1
phpstan ×1