w23wwhhi

157 经验值

有可能啊,我的是数据库名加上prefix,collection显示的时候就是显示关联的那个字段名就是带上了prefix,后来我就只能写原生sql

database.php:

'lws_edu'=> [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_EDU', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

env

DB_DATABASE_EDU=lws_edu

感觉是不是我那个类名或者文件名加了前缀之后就有问题了?

但改过来以后还是没连接到那个platform的数据库

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_order_goods.platform_user_order_id' in 'where clause' (SQL: select * from `user_order_goods` where `user_order_goods`.`platform_user_order_id` = 2 and `user_order_goods`.`platform_user_order_id` is not null)

噢,这个是写错了,我也没看到,不好意思啊

userOrder的model

class PlatformUserOrder extends Model
{
    protected $connection = 'lws_edu';
    protected $table = 'user_orders';
    protected $fillable = [
        'order_num',
        'user_id',
        'description',
        'account',
        'total_fee',
        'state',
        'user_consignee',
        'user_phone',
        'wx_num',
        'create_time',
        'pay_time',
        'wxpay_result_code',
        'wxpay_is_subscribe',
        'wxpay_bank_type',
        'wxpay_nonce_str',
        'is_removed',
        'transaction_id',
    ];

    public function user()
    {
        return $this->belongsTo('App\Models\LwsPlatform\PlatformUser','user_id','id');
    }

    public function userOrderGoods()
    {
        return $this->hasMany('App\Models\LwsPlatform\PlatformUserOrderGoods');
    }
}

userOrderGoods:

class PlatformUserOrderGoods extends Model
{
    protected $connection = 'lws_edu';
    protected $table = 'user_order_goods';
    protected $fillable = ['user_order_id','goods_id','order_goods_price','state','count','is_removed'];

    public function userOrder()
    {
        return $this->belongsTo('App\Models\LwsPlatform\PlatformUserOrderGoods','user_order_id','id');
    }
    
    
}

数据库显示的错误是这样的

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_order_goods.platform_user_order_id' in 'where clause' (SQL: select * from `user_order_goods` where `user_order_goods`.`platform_user_order_id` = 2 and `user_order_goods`.`platform_user_order_id` is not null)

platform就是我上面写的数据库别名,配置跟第一种写法是一样

我的写法就是第一种的,但userOrderGoods的关联方法是这样的

userOrderGoods:

public function userOrder()
    {
        return $this->belongsTo('App\Models\A\AUserOrderGoods','user_order_id','id');
    }

UserOrder:

public function userOrderGoods()
    {
        return $this->hasMany('App\Models\A\AUserOrderGoods');
    }

然后调用的时候就是

$orders = UserOrder::where('state',2)->get();
foreach($orders as $order) {
      $order->UserOrderGoods->id;
}

就说找不到a_user_order_id的字段,那是不是这种多库关联的就只能是用第二种写法的

我连接了一个新的数据库a,a里面有user_orders和user_order_goods两个表,

其中也建立了关联关系,user_order_goods里有user_order_id做关联字段,user_order里面有userOrderGoods做一对多关联,但我在页面的时候使用$order->userOrderGoods的时候,报字段名错误,显示查询的时候用了a_user_order_id这样带数据库前缀的字段,那我要在哪里设置才能把这个前缀去掉,用user_order_id做关联