laravel에서 sqlite로 연결된 DB를 mysql로 ​​전환하는 방법

10163 단어 MySQL라라벨sqlite

전제 조건



mysql -u root로 연결
테이블은 posts 테이블을 사용 (id.title 전용 구조)
모델은 Post.php
mysql> desc posts;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| title | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
mysql> select *from posts;
+------+--------+
| id   | title  |
+------+--------+
|    1 | Yamada |
+------+--------+

index.blade.php


  @forelse ($posts as $post)

  <tbody>
    <tr>

      <th scope="row">1</th>
      <td>  <a href="{{ action('PostsController@show', $post) }}">{{ $post->title }}</a>
</td>
      <td> <a href="{{ action('PostsController@edit', $post) }}" class="edit">[Edit]</a>
</td>

      <td> <a href="#" class="del" data-id="{{ $post->id }}">[x]</a>
      <form method="post" action="{{ url('/posts', $post->id) }}" id="form_{{ $post->id }}">
      {{ csrf_field() }}
      {{ method_field('delete') }}
    </form>
</td>

    </tr>
    @empty
@endforelse

Postscontroller



색인 작업
 public function index() { 
     $posts = Post::all(); 
     return view('posts.index', ['posts' => $posts]);
      }


.env



.env 변경 후 $ php artisan config : cache 실행

sqlite 연결시

DB_CONNECTION=sqlite

#DB_CONNECTION=mysql
#DB_HOST=127.0.0.1 
#DB_PORT=3306
#DB_DATABASE=myblog
#DB_USERNAME=root
#DB_PASSWORD=

mysql 연결시


#DB_CONNECTION=sqlite

DB_CONNECTION=mysql
DB_HOST=127.0.0.1  
DB_PORT=3306
DB_DATABASE=DB名
DB_USERNAME=root
DB_PASSWORD=

database.php는 특별히 언급하지 않고



config/database


  'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'myblog'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],


표시되었습니다!

좋은 웹페이지 즐겨찾기