【问题标题】:Saving array of dynamic fields data to DB using oneToMany relationship使用 oneToMany 关系将动态字段数据数组保存到 DB
【发布时间】:2019-04-22 15:59:31
【问题描述】:

我一直在尝试以数组的形式保存来自动态生成的字段的数据。我有一个客户表的 oneToMany 关系。

我尝试循环遍历每个字段,但无法实现,如果我错了,请纠正我。

public function store(Request $request)
{
    $res = $request->all();
    $res['address'] = implode(' ', array_values($request->address));
    $customer = Customer::create($res);
    if ($res) {
        $customerData = [];
        foreach ($request->department_name as $key => $n) {
            $customerData = array(
                'department_name' => $request->department_name[$key],
                'person_name' => $request->person_name[$key],
                'person_number' => $request->person_number[$key],
                'person_email' => $request->person_email[$key],
                'notification_flag' => !isset($request->notification_flag[$key]) ? 0 : $request->notification_flag[$key] === "on" ? 1 : 0,
                'custinvoice_noti' => !isset($request->outstanding[$key]) ? 0 : $request->outstanding[$key] === "on" ? 1 : 0,
                'invoice_noti' => !isset($request->invoice[$key]) ? 0 : $request->invoice[$key] === "on" ? 1 : 0,
            );

            $deptModel[] = new Department($customerData);
            $customer->department()->saveMany($deptModel);
        }
    }
    return redirect('admin/customers');
}

客户模型和部门模型有如下关系。

class Customer extends Model
{
protected $fillable = ['owner_name', 'address', 'country', 'state', 'city', 'pincode', 'number', 'correspondance_check'];

public function department()
{
    return $this->hasMany('App\Department');
}
}

部门模型。

class Department extends Model
{
protected $fillable = ['customer_id', 'department_name', 'person_name', 'person_number', 'person_email', 'notification_flag', 'notification_type'];

public function customer()
{
    return $this->belongsTo('App\Customer');
}
}

【问题讨论】:

  • 请将图片中的文字替换为图片中包含的文字。文本图像在小屏幕上无法搜索、访问或阅读。

标签: php laravel laravel-5 voyager


【解决方案1】:

公共函数存储(请求 $request) { $customer = 新客户();

    $customer->owner_name = $request['owner_name'];
    $customer->country = $request['country'];
    $customer->state = $request['state'];
    $customer->city = $request['city'];
    $customer->pincode = $request['pincode'];
    $customer->number = $request['number'];
    $customer->correspondance_check = $request['correspondance_check'];
    $res = $customer->save();
    $cus = Customer::where(['id'=> $res->id])->firstOrFail();
    $dep = new Department();
    $dep->customer()->associate($cus);
    $dep->save();

    return redirect('admin/customers');
    // return response()->json($customerData);
}

Customer model and  Department model have the following relationship.

class Customer extends Model
{

protected $fillable = ['owner_name', 'address', 'country', 'state', 'city', 'pincode', 'number', 'correspondance_check'];

public function department()
{
    return $this->hasMany('App\Department');
}

}

//Department Model.

class Department extends Model
{
protected $fillable = ['customer_id', 'department_name', 'person_name', 'person_number', 'person_email', 'notification_flag', 'notification_type'];

public function customer()
{
    return $this->belongsTo('App\Customer');
}
}

【讨论】:

  • 问题是你可以在转储图像中看到,我有动态创建的字段并提供值数组。
猜你喜欢
  • 2016-07-09
  • 2018-06-09
  • 2013-06-03
  • 1970-01-01
  • 2017-08-31
  • 2010-10-23
  • 2014-01-12
  • 1970-01-01
  • 2021-03-09
相关资源
最近更新 更多