我有一个名为public
的架构。在这个模式中,我有一个名为currencies
的表。
<?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',
];
}
由于$table
设为public.currencies
,所有功能都完美无缺。我可以通过存储库或直接在我的控制器中执行所有CRUD操作。
似乎被破坏的是尝试使用exists
规则。
在我的验证器中我有:
'name' => [
'required',
'exists:public.currencies',
],
这只是一段摘录。最终的验证将会复杂得多,但问题是当我称之为public.currencies
时,Laravel无法看到该表。
显然语法
schema.table
不起作用。有什么想法吗?我可以使用自定义验证并使用自定义存储库方法来检查是否存在,但我想使用本机方法。
答案 0 :(得分:0)
该错误表示您的数据库配置没有为公开连接定义键值。检查您的config/databases.php
文件&amp;指定与 public 的连接作为连接名称(例如,使用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,
],
],
或者,您也可以省略验证规则中的连接,如果您打算验证的连接是Laraval文档中提到的默认连接:
自定义数据库连接
有时,您可能需要为Validator进行的数据库查询设置自定义连接。如上所示,将
unique:users
设置为验证规则将使用默认数据库连接来查询数据库。要覆盖它,请使用“dot”语法指定连接后跟表名: