2016-09-19 58 views
0

我有一个架构,称为public。在这个模式中,我有一个名为currencies的表。Laravel 5.1 Eloquent - 引用来自验证的不同架构说架构不存在

<?php 
declare(strict_types=1); 

namespace Lapis\Domain\Currency; 

use Lapis\Domain\Core\Database\Eloquent\Model; 

/** 
* Class EloquentCurrency 
* 
* @package Lapis\Domain\Currency 
*/ 
class EloquentCurrency extends Model implements CurrencyInterface 
{ 
    /** 
    * Indicates if the IDs are auto-incrementing. 
    * 
    * @var bool 
    */ 
    public $incrementing = false; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'public.currencies'; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'name', 
     'symbol', 
     'code', 
     'decimal_places', 
    ]; 
} 

一切完美,由于设置$tablepublic.currencies。我可以通过存储库或直接在我的控制器中执行所有CRUD操作。

什么似乎被打破是试图使用exists规则。

在我的验证,我有:

'name' => [ 
    'required', 
    'exists:public.currencies', 
], 

这仅仅是一个节选。最终的验证将会更加复杂,但问题在于,当我称之为public.currencies时,Laravel无法看到表格。

显然语法

schema.table

不起作用。任何想法为什么?我可以解决这个我使用自定义验证和使用自定义存储库方法来检查是否存在,但我想使用本地方法。

enter image description here

回答

0

的错误表明您的数据库配置有一个公共连接没有定义键值。检查config/databases.php文件&指定与公共作为连接名称(例如使用MySQL作为驱动程序)连接:

'connections' => [ 

    ... 

    'public' => [ 
     'driver' => 'mysql', 
     'host' => env('DB_HOST', 'localhost'), 
     'port' => env('DB_PORT', '3306'), 
     'database' => env('DB_DATABASE', 'forge'), 
     'username' => env('DB_USERNAME', 'forge'), 
     'password' => env('DB_PASSWORD', ''), 
     'charset' => 'utf8', 
     'collation' => 'utf8_unicode_ci', 
     'prefix' => '', 
     'strict' => true, 
     'engine' => null, 
    ], 
], 

另外,您还可以ommit从您的验证规则的连接,如果连接你打算与验证是在Laraval文档提到你的默认连接:

自定义数据库连接

有时候,您可能需要为Validator所做的数据库查询设置自定义连接。如上所示,将unique:users设置为验证规则将使用默认数据库连接来查询数据库。若要覆盖此设置,请使用“点”语法指定表格名称后面的连接: