在 Laravel 中,关系的检查引用了 createMany
方法。有关此方法的更多信息,请参见此链接和来自 Laravel 的文档。
到目前为止,我的示例如下所示。
我有两个模型:Pricing
和 AvailableService
模型。
Pricing 模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pricing extends Model
{
protected $fillable = ["name", "price"];
public function available(){
return $this->hasMany(AvailableService::class, "pricing_id", "id");
}
}
AvailableServiceMode
的外观如下:
namespace App;
use Illuminate\Database\Eloquent\Model;
class AvailableService extends Model
{
protected $fillable = ["pricing_id", "service_id"];
public function service(){
return $this->belongsTo(Service::class, "service_id", "id");
}
}
因此,createMany
操作看起来像这样
$insertMany = Pricing::create(['name'=>request('name')]);
$insertMany->available()->createMany([
['service_id'=>1],
['service_id'=>2],
['service_id'=>3],
['service_id'=>4],
['service_id'=>5],
]);
它对于IT技术相关内容来说是有效的,你也可以尝试一下。谢谢