Laravel:如何借助中间表处理一对多关联关系
Laravel 基于中间表实现一对多关联及工厂数据创建问题解决
背景与问题
现有数据库包含三张表:customers、vehicles及中间表customer_vehicle,结构如下:
Schema::create('customers', function (Blueprint $table) { $table->id(); $table->string('name'); }); Schema::create('vehicles', function (Blueprint $table) { $table->id(); $table->string('name'); }); Schema::create('customer_vehicle', function (Blueprint $table) { $table->id(); $table->int('customer_id'); $table->int('vehicle_id'); });
实际业务逻辑为一对多:一个客户可拥有多辆车,一辆车仅归属一个客户。最初配置为多对多关联可正常运行,但希望通过$vehicle->customer直接获取车辆所属客户,因此在Vehicle模型中使用hasOneThrough实现关联:
class Vehicle extends Model { public function customer() { return $this->hasOneThrough( Customer::class, CustomerVehiclePivot::class, 'vehicle_id', 'id', 'id', 'customer_id' ); } }
关联查询可正常使用$vehicle->customer和$customer->vehicles获取数据,但使用模型工厂创建关联数据时,执行Vehicle::factory()->for(Customer::factory())->create()会抛出错误:
Call to undefined method Illuminate\Database\Eloquent\Relations\HasOneThrough::getOwnerKeyName()
可行的工厂数据创建方式
方式一:使用hasAttached通过客户工厂创建关联
Customer::factory() ->hasAttached( Vehicle::factory()->count(3) ) ->create()
方式二:创建客户后手动关联车辆
Customer::factory()->create()->vehicles()->attach(Vehicle::factory()->count(3)->create());
原因说明
hasOneThrough属于间接关联关系,Eloquent工厂的for方法仅支持hasOne、belongsTo这类直接关联,无法适配hasOneThrough,因此需要借助中间表的attach方法来完成关联数据的创建。
内容的提问来源于stack exchange,提问作者Josh
相关产品推荐
相关产品推荐

