orm 使用join 别的数据库的表不能使用?????

ShoppingCard::query()
->with([‘shoppingCardBatches’=>function($query){
$query->select(
‘id’,
‘company’,
‘remark’,
‘end_time as batch_end_time’,
‘amount as batch_amount’,
‘gift_amount as batch_gift_amount’,
‘start_time as batch_start_time’
);
},’userShoppingCards’=>function($query){
$query->select(
‘card_id’,
‘amount as card_amount’,
‘remark as sremark’,
‘start_time as card_start_time’,
‘end_time as card_end_time’,
‘create_time as usc_time’,
‘status as card_status’,
‘lock_end_time’,
‘balance’,
‘card_type’,
‘gift_amount as card_gift_amount’
);
}])
->joinSub($walkingVoucher, ‘walking_voucher’, function ($join) {
$join->on(‘shopping_cards.batch_id’, ‘=’, ‘walking_vouchers.shopping_card_batches_id’);
})
//->join(‘mysql.walking_vouchers’,’walking_vouchers.shopping_card_batches_id’,’=’,’shopping_cards.batch_id’)
->when(!empty($params[‘card_id’]),function ($query)use ($params){
$query->where(‘shopping_cards.id’,’like’,’%’.$params[‘card_id’].’%’);
})
->when(!empty($params[‘card_status’]),function ($query)use ($params){
$query->whereHas(‘userShoppingCards’,function ($query)use ($params){
$query->where(‘status’,$params[‘card_status’]);
});
})
->select(
‘shopping_cards.id’,
‘shopping_cards.batch_id’,
‘shopping_cards.id as card_id’,
‘shopping_cards.password’,
‘shopping_cards.user_id’,
‘shopping_cards.status as card_base_status’,
‘shopping_cards.end_time as custom_end_time’
)
// ->orderByDesc(‘walking_vouchers.create_time’)
->paginate($params[‘limit’]??20); orm 使用join 别的数据库的表不能使用?????

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 8

chatGPT
在 Laravel 的 Eloquent ORM 中,跨数据库 JOIN 可以通过定义关联关系并使用查询构建器实现。以下是一个示例:

首先,我们需要创建两个模型,分别对应两个数据库中的表。这里假设我们有两个数据库 db1db2,以及两个表 table1table2

  1. app/Models 目录下创建一个名为 Table1 的模型:

Copy

// app/Models/Table1.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Table1 extends Model
{
    protected $connection = 'db1';
    protected $table = 'table1';

    public function table2()
    {
        return $this->hasOne(Table2::class, 'id', 'id');
    }
}

这里,我们将模型 Table1 的数据库连接设置为 db1,并定义了一个关联关系 table2(),表示在 table2 中有一个与 table1 相关联的记录。

  1. 同样地,在 app/Models 目录下创建一个名为 Table2 的模型:

Copy

// app/Models/Table2.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Table2 extends Model
{
    protected $connection = 'db2';
    protected $table = 'table2';

    public function table1()
    {
        return $this->belongsTo(Table1::class, 'id', 'id');
    }
}

这里,我们将模型 Table2 的数据库连接设置为 db2,并定义了一个关联关系 table1(),表示在 table1 中有一个与 table2 相关联的记录。

  1. 确保在 config/database.php 中定义了这两个数据库连接:

Copy

// config/database.php
'connections' => [
    'db1' => [
        'driver' => 'mysql',
        'host' => env('DB1_HOST', '127.0.0.1'),
        'port' => env('DB1_PORT', '3306'),
        'database' => env('DB1_DATABASE', 'db1'),
        'username' => env('DB1_USERNAME', 'forge'),
        'password' => env('DB1_PASSWORD', ''),
        // ... 其他配置 ...
    ],

    'db2' => [
        'driver' => 'mysql',
        'host' => env('DB2_HOST', '127.0.0.1'),
        'port' => env('DB2_PORT', '3306'),
        'database' => env('DB2_DATABASE', 'db2'),
        'username' => env('DB2_USERNAME', 'forge'),
        'password' => env('DB2_PASSWORD', ''),
        // ... 其他配置 ...
    ],
],
  1. 现在,我们可以在控制器或其他地方使用跨数据库 JOIN 查询:

Copy

use App\Models\Table1;

$results = Table1::with('table2')->get();

这将执行跨数据库 JOIN 查询,并获取 table1table2 中的相关记录。你可以根据需要修改查询条件和返回的字段。

Regenerate response

10个月前 评论
lijizheng (楼主) 10个月前
哪吒的狗腿子 (作者) 10个月前
哪吒的狗腿子 (作者) 10个月前
lijizheng (楼主) 10个月前

分不同的服务器连接, 已经应用到了多个项目实际运行半年有余

<?php

namespace App\Traits;

trait SlaveConnection
{
    /**
     * Get the current connection name for the model.
     *
     * @return string
     */
    public function getConnectionName(): string
    {
        return config('database.slave');  // 自己根据实际情况修改
    }

    /**
     * Create a new instance of the related model.
     *
     * @param  string  $class
     * @return mixed
     */
    public function newRelatedInstance($class)
    {
        $instance = parent::newRelatedInstance($class);

        $instance->setConnection(config('database.default'));

        return $instance;
    }
}
<?php

namespace App\Models;

class Printer extends Model {
       use \App\Traits\SlaveConnection;
}
10个月前 评论
lijizheng (楼主) 10个月前
小学毕业生 (作者) 10个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!