/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate request $validateAgent = Validator::make($request->get('Agent'), Agent::$rules); $validationMessages = []; if ($validateAgent->fails() or count($validationMessages) > 0) { $validationMessages = array_merge_recursive($validateAgent->messages()->toArray(), $validationMessages); return redirect()->back()->withErrors($validationMessages)->withInput(); } $user = $request->user(); // insert Agent $agent = new Agent(); //$agent->key = Common::createKeyURL($request->input('Agent.name')); $agent->name = $request->input('Agent.name'); $agent->email = $request->input('Agent.email'); $agent->mobile = $request->input('Agent.mobile'); // get thumnail $agent->thumnail = $request->input('Agent.thumnail'); $agent->priority = $request->input('Agent.priority'); $agent->active = $request->input('Agent.active'); $agent->created_by = $user->name; $agent->updated_by = $user->name; $agent->save(); // save attachments if ($request->input('Agent.attachments') != "") { $requestAttachments = explode(',', $request->input('Agent.attachments')); $attachments = []; foreach ($requestAttachments as $key => $value) { array_push($attachments, new Attachment(['entry_id' => $agent->id, 'table_name' => 'agents', 'path' => $value, 'priority' => 0, 'is_publish' => 1])); } if (count($attachments) > 0) { $agent->attachments()->saveMany($attachments); } } return redirect()->route('admin.agents.index'); }