lundi 22 avril 2019

Saving array of dynamic fields data to DB using oneToMany relationship

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

enter image description here I have tried to loop through each field but i am unable to acheive it , please correct me if i am wrong.

public function store(Request $request)
{
    $temparr = [];
    $customer = new Customer();
    $department = new DepartMent();
    dd($request->all());
    /* Testing for empty/null values */
    foreach ($request->all() as $key => $value) {
        if (is_array($value)) {
            $temparr = array_filter($value);
            $request[$key] = $temparr;
        }
    }

    /* Validations */
    $validatedData = $request->validate([
        'owner_name' => 'required',
        'pincode' => 'numeric|nullable',
        'number' => 'numeric|nullable',
    ]);

    $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();

    $result = Customer::select('id')->where('owner_name', $request->owner_name)->first();

    $department->customer_id = $result->id;
    $department->department_name = $request->department_name;
    $department->person_name = $request->person_name;
    $department->person_number = $request->person_number;
    $department->person_email = $request->person_email;
    $department->notification_flag = $request->notification_check;
    $department->notification_type = $request->notification_type;

    foreach ($department->department_name as $key => $n) {
        // DB::table('users_dependents')->insert(
        $customer->department()->saveMany(
            array(
                'customer_id' => $department->customer_id[$key],
                'department_name' => $department->department_name[$key],
                'person_name' => $department->person_name[$key],
                'person_number' => $department->person_number[$key],
                'person_email' => $department->person_email[$key],
                'notification_flag' => $department->notification_flag[$key],
                'notification_type' => $department->notification_type[$key],

            )
        );
    }

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

Customer model and Department model has 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');
}
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire