0

I have been trying to save the data coming from the dynamically generated fields in the form of an array. I have a oneToMany relationship for the customer table.

enter image description here

I have tried to loop through each field but I am unable to achieve it, please correct me if I am wrong.

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');
}

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');
}
}

enter image description here

rahul.sharma
  • 457
  • 6
  • 20
  • Please replace the image of text with the text that the image contains. Images of text are not searchable, accessible, or easy to read on small screens. – Jason Aller Apr 23 '19 at 01:55

1 Answers1

0

public function store(Request $request) { $customer = new 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');
}
}
Sok Chanty
  • 1,678
  • 2
  • 12
  • 22
  • the problem is as u can see in the dump image , i have fields which are dynamically created and gives array of values . – rahul.sharma Apr 23 '19 at 10:11