【发布时间】:2019-11-02 06:54:30
【问题描述】:
我一直在尝试将数据保存在数据库中,但此错误不断出现:
(2/2) QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `stores` (`name`, `description`) values (El café de mi esquina , Es lindo))
这是控制器上的代码:
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store()
{
$nuevoLocal= new Store();
$nuevoLocal->name="El café de mi esquina ";
$nuevoLocal->description="Es lindo";
$nuevoLocal-> save();
}
网络:
Route::get('/agregarNegocio', "storeController@store");
型号:
命名空间应用程序;
use Illuminate\Database\Eloquent\Model;
class store extends Model
{
public $guarded =[];
public $timestamps=false;
}
环境:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cafe
DB_USERNAME=root
DB_PASSWORD=root
数据库名称是cafe,它有一个表存储有ID(主键),名称和描述
如果放 var_dump($nuevoLocal);出口;就在 save() 之前,这是我得到的:
object(App\store)#158 (25) { ["guarded"]=> array(0) { } ["timestamps"]=> bool(false) ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(false) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(2) { ["name"]=> string(23) "El café de mi esquina " ["description"]=> string(8) "Es lindo" } ["original":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["events":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
【问题讨论】:
-
错误是
Connection refused,因此它是数据库的配置之一,主要是DB_HOST,但也可以是DB_PORT。试试DB_HOST=localhost -
您是否正确配置了 .env 文件。当你连接到你的数据库时,你使用
Username = root&Password = root -
尝试修改 DB_HOST=localhost 但不起作用):
-
确保你的 mysql 正在运行。好像mysql关了
-
mysql 正在运行!
标签: php database laravel localhost laravel-artisan