laravel:多mysql数据库(10.27.0 )
一,相关文档
https://learnku.com/docs/laravel/10.x/database/14882#2cd405
二,php代码
1,编辑.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gonews
DB_USERNAME=yourusername
DB_PASSWORD=yourpassword
CO_DB_CONNECTION=mysql
CO_DB_HOST=127.0.0.1
CO_DB_PORT=3306
CO_DB_DATABASE=gotouch
CO_DB_USERNAME=yourusername
CO_DB_PASSWORD=yourpassword
2,编辑config/database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
'mysql' => [ 'driver' => 'mysql' , 'url' => env( 'DATABASE_URL' ), 'host' => env( 'DB_HOST' , '127.0.0.1' ), 'port' => env( 'DB_PORT' , '3306' ), 'database' => env( 'DB_DATABASE' , 'forge' ), 'username' => env( 'DB_USERNAME' , 'forge' ), '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' ), ]) : [], ], 'co_mysql' => [ 'driver' => 'mysql' , 'url' => env( 'CO_DATABASE_URL' ), 'host' => env( 'CO_DB_HOST' , '127.0.0.1' ), 'port' => env( 'CO_DB_PORT' , '3306' ), 'database' => env( 'CO_DB_DATABASE' , 'forge' ), 'username' => env( 'CO_DB_USERNAME' , 'forge' ), 'password' => env( 'CO_DB_PASSWORD' , '' ), 'unix_socket' => env( 'CO_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' ), ]) : [], ], |
3,生成model
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:model Comment
INFO Model [app/Models/Comment.php] created successfully.
4,model的代码:
app/Models/News.php:未指connection时使用默认的mysql连接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class News extends Model { use HasFactory; //定义关联的数据表 protected $table = 'news' ; //定义主键 protected $primaryKey = 'news_id' ; // 时间字段是否自动管理(created_at 和 updated_at字段) public $timestamps = false; //添加数据 public function add( $row ){ $int = $this ->insert( $row ); return $int ; } //读取全部数据 public function getAll() { return $this ->get()->toarray(); } //分页读取数据 public function getPage( $offset , $limit ){ $rows = $this ->orderBy( 'news_id' , 'desc' )->offset( $offset )->limit( $limit )->get(); return $rows ; } } |
app/Models/Comment.php:代码中指定连接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use HasFactory; //connection protected $connection = 'co_mysql' ; //定义关联的数据表 protected $table = 'it_comment' ; //定义主键 protected $primaryKey = 'cm_id' ; // 时间字段是否自动管理(created_at 和 updated_at字段) public $timestamps = false; //读取全部数据 public function getAll() { return $this ->get()->toarray(); } //读取一条数据 public function getOneById( $cm_id ) { return $this ->where( 'cm_id' , $cm_id )->get()->toarray(); } } |
5,在controller中访问:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//获取请求参数 public function home(Request $request ){ //得到新闻 $modelNews = new News(); $rowsNews = $modelNews ->getPage(0,1); //得到评论 $modelComment = new Comment(); $rowsComment = $modelComment ->getOneById(3); //返回数据 $data = [ "news" => $rowsNews , "comment" => $rowsComment , ]; return Result::Success( $data ); } |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/23/laravel-duo-mysql-shu-ju-ku-10-27-0/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
三,测试效果:
显示了来自两个数据库中的数据:
四,查看laravel框架的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0