Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel立即插入类别和x产品_Php_Laravel 5.3_Laravel Eloquent - Fatal编程技术网

Php Laravel立即插入类别和x产品

Php Laravel立即插入类别和x产品,php,laravel-5.3,laravel-eloquent,Php,Laravel 5.3,Laravel Eloquent,我有一个模型类别和一个模型产品。现在,我想插入一个类别和10个产品耦合到该类别一次 我怎样才能做到这一点 class Categroy extends Model { public function products() { return $this->hasMany('App\Http\Models\Product'); } } class Product extends Model{ public function categories() {

我有一个模型类别和一个模型产品。现在,我想插入一个类别和10个产品耦合到该类别一次

我怎样才能做到这一点

class Categroy extends Model
{
    public function products() {
        return $this->hasMany('App\Http\Models\Product');
    }
}

class Product extends Model{
    public function categories() {
        return $this->hasMany('App\Http\Models\Category');
    }
}

这是正确记录的。也许,你可以这样做

$category = Category::create([
    'name' => 'Properties'
]);

$products = $category->products()->create([
    'name' => 'Apartment',
    'name' => 'Townhouse'
]);

这是一种方法,但我想一次插入1个数组/对象,其中包含类别和产品,这也是可行的吗