简体   繁体   English

Laravel Eloquent saveMany 不接受第二个属性

[英]Laravel Eloquent saveMany does not accept second attribute

As I think it is typically as the documentaion I have tried to use saveMany regarding its parameters as Model objects.我认为它通常作为文档,我尝试使用saveMany将其参数作为模型对象。

I have a model named Set and it hasMany Equipment so, from the from that creates a set I submit many fields of Equipment in-order to be saved as the following:我有一个名为模型Set ,它hasMany Equipment等等,从从创建一组我提交的许多领域Equipment按顺序保存为如下:

//Store method of SetController
$set = new Set();
        $set->title = request('title');
        $set->eqtype_id = request('eqtype');
        $set->product_id = request('product');
        $set->parts_count = request('parts_count');
        $eq = request('equipments');
        $set->equipments()->saveMany(array_map(function($n){return new \App\Equipment(['title' => $n, 'eqtype_id' => request('eqtype')]);}, $eq));
        $set->save();

However, I always get the error message about that eqtype_id does not has a default value:但是,我总是收到有关eqtype_id没有默认值的错误消息:

SQLSTATE[HY000]: General error: 1364 Field 'eqtype_id' doesn't have a default value (SQL: insert into equipments ( title , set_id , updated_at , created_at ) values (A-1, , 2017-03-18 12:07:30, 2017-03-18 12:07:30)) SQLSTATE[HY000]:一般错误:1364 字段“eqtype_id”没有默认值(SQL:插入equipmentstitleset_idupdated_atcreated_at )值(A- set_id 12:07 :30, 2017-03-18 12:07:30))

I thought that, may be, request('eqtype') is not accessible from the array_map callback, so I replaced it with a fixed number as follows:我认为,可能是request('eqtype')无法从array_map回调中访问,因此我将其替换为固定数字,如下所示:

$set->equipments()->saveMany(array_map(function($n){return new \App\Equipment(['title' => $n, 'eqtype_id' => 1]);}, $eq));

But also the error is same, it seems like a problem with either saveMany or array_map .但错误也是一样的,这似乎是saveManyarray_map的问题。 I don't know how to fix this issue!我不知道如何解决这个问题!

Notice注意

The model Equipment is related with the both model Set and Eqytpe as follows:模型Equipment与模型SetEqytpe的关系如下:

    //Equipment model
       <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Equipment
 *
 * @mixin \Eloquent
 */
class Equipment extends Model
{
    public $table = "equipments";
    protected  $fillable = ['title'];
    public function set()
    {
      return $this->belongsTo(Set::class);
    }

    public function eqtype()
    {
      return $this->belongsTo(Eqtype::class);
    }

I kind of had the same problem and this is what I did to fix it:我遇到了同样的问题,这就是我为解决它所做的:

I had a One to Many relationship between a Product and Image and I wanted on a product creation add all the images related to it我在产品和图像之间有一对多的关系,我想在产品创建中添加与其相关的所有图像

// Create post
public function store(Request $request)
{
        $newProduct = Product::create([
        'name' => $request->name,
        'description' => $request->description,
        'price' => $request->price,
        'stock_items' => $request->stock_items,
    ]);


    for ($i = 0; $i < count($request->image_path); $i++) {
        $newProduct->image()->saveMany([

            new Image(['image_path' => $request->image_path[$i]])

        ]);
    }
}

Don't forget the relationship functions & to create multiple input with the same name : name='image_path[]'不要忘记关系函数 & 以创建多个具有相同名称的输入:name='image_path[]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM