Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. public function store(Request $request)
  2. {
  3. $temparr = [];
  4. $customer = new Customer();
  5. $department = new DepartMent();
  6. dd($request->all());
  7. /* Testing for empty/null values */
  8. foreach ($request->all() as $key => $value) {
  9. if (is_array($value)) {
  10. $temparr = array_filter($value);
  11. $request[$key] = $temparr;
  12. }
  13. }
  14.  
  15. /* Validations */
  16. $validatedData = $request->validate([
  17. 'owner_name' => 'required',
  18. 'pincode' => 'numeric|nullable',
  19. 'number' => 'numeric|nullable',
  20. ]);
  21.  
  22. $customer->owner_name = $request['owner_name'];
  23. $customer->country = $request['country'];
  24. $customer->state = $request['state'];
  25. $customer->city = $request['city'];
  26. $customer->pincode = $request['pincode'];
  27. $customer->number = $request['number'];
  28. $customer->correspondance_check = $request['correspondance_check'];
  29.  
  30. $res = $customer->save();
  31.  
  32. $result = Customer::select('id')->where('owner_name', $request->owner_name)->first();
  33.  
  34. $department->customer_id = $result->id;
  35. $department->department_name = $request->department_name;
  36. $department->person_name = $request->person_name;
  37. $department->person_number = $request->person_number;
  38. $department->person_email = $request->person_email;
  39. $department->notification_flag = $request->notification_check;
  40. $department->notification_type = $request->notification_type;
  41.  
  42. foreach ($department->department_name as $key => $n) {
  43. // DB::table('users_dependents')->insert(
  44. $customer->department()->saveMany(
  45. array(
  46. 'customer_id' => $department->customer_id[$key],
  47. 'department_name' => $department->department_name[$key],
  48. 'person_name' => $department->person_name[$key],
  49. 'person_number' => $department->person_number[$key],
  50. 'person_email' => $department->person_email[$key],
  51. 'notification_flag' => $department->notification_flag[$key],
  52. 'notification_type' => $department->notification_type[$key],
  53.  
  54. )
  55. );
  56. }
  57.  
  58. return redirect('admin/customers');
  59. // return response()->json($customerData);
  60. }
  61.  
  62. class Customer extends Model
  63. {
  64. protected $fillable = ['owner_name', 'address', 'country', 'state', 'city', 'pincode', 'number', 'correspondance_check'];
  65.  
  66. public function department()
  67. {
  68. return $this->hasMany('AppDepartment');
  69. }
  70. }
  71.  
  72. class Department extends Model
  73. {
  74. protected $fillable = ['customer_id', 'department_name', 'person_name', 'person_number', 'person_email', 'notification_flag', 'notification_type'];
  75.  
  76. public function customer()
  77. {
  78. return $this->belongsTo('AppCustomer');
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement