在laravel项目之外使用 Eoquent 并增加 laravel_ide_helper 提示

  1. composer 增加database包 composer require illuminate/database
  2. 如果之前没使用过composer,需要加载:
    1. // 加载composer autoload
      require_once COMPOSER_VENDOR.'/autoload.php';
      // 数据库连接信息
      $connections = $this->getConnections();
      // Eloquent ORM
      $capsule = new Capsule;
      // $name 为多连接配置时,通过connection 指定当前连接
      foreach ($connections as $name => $connection) {
      $capsule->addConnection($connection, $name);
      }
      $capsule->bootEloquent();
    2. 其中数据库配置的格式为:
      $formatConfig = [
      'driver'    => 'mysql',
      'database'  => 'dbname',
      'username'  => 'user_name',
      'password'  => 'password',
      'port'  => 'port',
      ];
    3. 如果是读写分离则参考$readWriteConfig,注意格式:
      $readWriteConfig = [
      'driver'    => 'mysql',
      'write' => [
      'host' => 'xxx.xxx.xxx.xxx'
      ],
      'read' => [
      ['host' => 'yyy.yyy.yyy.yyy'],
      ['host'=>'zzz.zzz.zzz.zzz']
      ],
      'database'  => 'dbname',
      'username'  => 'user_name',
      'password'  => 'password',
      'port'  => 'port',
      ];
  3. phpstorm laravel_ide_helper:增加 _ide_helper.php文件,参考laravel已生成的进行部分复制。但是这个时候还无法提示,需要在 model 父类文件中增加备注 @mixin \Eloquent

    use Illuminate\Database\Eloquent\Model as Eloquent;
    
    /**
    * _ide_helper
    *
    * @mixin \Eloquent
    */
    class BaseEloquent extends Eloquent
    {
    // 设置连接库
    protected $connection = 'default';
    }
评论数量: 0

0
点赞
248
浏览
0
评论

贡献 67