如何在数据库中输入数据?

时间:2019-06-19 13:48:09

标签: php database laravel localhost artisan

我一直试图在数据库中保存数据,但是此错误不断出现:

(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;

}

env:

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) { } }

2 个答案:

答案 0 :(得分:0)

尝试将DB_CONNECTION127.0.0.1切换到localhost。显然,在Laravel中使用127.0.0.1会遇到一些问题。

答案 1 :(得分:0)

我遇到了同样的问题,结果发现Port不是标准端口。在SQL Server上尝试一下

types=[type.continuous]