public function store(Request $request) { try { $type = $request->input('type'); $e = EntityType::findOrFail($type); $n = new Entity(); $n->type_id = $e->id; $n->name = $request->input('name'); $n->save(); foreach ($e->attributes as $attribute) { $name = $attribute->systemName(); if ($request->has($name)) { $a = new Attribute(); $a->entity_id = $n->id; $a->type_id = $attribute->id; $a->value = $request->input($name); $a->save(); } } Session::flash('message_type', 'success'); Session::flash('message', 'Nuovo elemento salvato correttamente.'); } catch (\Expection $e) { Session::flash('message_type', 'danger'); Session::flash('message', 'Elemento non salvato correttamente.'); } return redirect('entities/' . $e->id); }
public function attribute($type) { if (is_string($type)) { $name = $type; } else { $name = $type->slug; } $a = Attribute::where('entity_id', '=', $this->id)->whereHas('type', function ($query) use($name) { $query->where('slug', '=', $name); })->first(); if ($a == null) { $a = new Attribute(); $a->entity_id = $this->id; $a->type_id = $this->type->attribute($name)->id; $a->value = ''; $a->save(); } return $a; }
public function putUpdate(Request $request, User $user) { if ($request->header('Authorization') == null) { return response()->json(['error' => 'Authorization header not available'], 422); } try { $access_token = $request->header('Authorization'); $hash = explode(':', $access_token); $id = $request->input('id'); $userInput = $request->input('user'); /* * Check if the header is correct */ if (count($hash) < 2) { return response()->json(['error' => 'Invalid Authorization header.'], 422); } else { if ($id != $hash[0]) { return response()->json(['error' => 'Invalid Request.'], 422); } } $userDet = $user->where('id', $id)->where('access_token', $hash[1])->first(); /* * Check if data can be extracted from the provided header and the id */ if ($userDet) { $attribute = new AttributeController(); $userattr = $userDet->attributes; /* * Get the attribute list array of a user */ $attrs = $attribute->getInit(); $content = ['email' => $userDet->email, 'user_id' => $id, 'avatar_image' => $userDet->avatar_image]; $content = array_merge($content, $attrs); /* * Update each attribute provided by the user */ foreach ($userInput as $key => $eachattr) { $attrValue = $eachattr; if ($key == 'username') { $userDet->username = $attrValue; $userDet->save(); } else { if ($key == 'avatar_image') { if (!$eachattr) { $userDet->avatar_image = ""; continue; } $date = date('Y_m_d_H_i_s'); $filename = $date . $user->name; $uploadfile = $userDet->upload($attrValue, $filename); $userDet->avatar_image = $uploadfile == 'Error' ? "" : $uploadfile; $userDet->save(); } else { $updateAttr = Attribute::where('user_id', $id)->where('key', $key)->first(); if (!$updateAttr) { $updateAttr = new Attribute(); $updateAttr->user_id = $id; $updateAttr->key = $key; } if (is_array($eachattr)) { $attrValue = json_encode($eachattr); } $updateAttr->value = $attrValue; $updateAttr->save(); // $content[$updateAttr->key] = $updateAttr->value; } } $content[$key] = $attrValue; } foreach ($userattr as $eachattr) { $attrValue = $eachattr->value; if ($eachattr->value == "true") { $attrValue = true; } else { if ($eachattr->value == "false") { $attrValue = false; } else { if ($eachattr->value == "null") { $attrValue = null; } else { if (json_decode($eachattr->value) != null) { $attrValue = json_decode($eachattr->value); } } } } $content[$eachattr->key] = $attrValue; } return response()->json($content, 200); } else { return response()->json(['error' => 'Invalid Request.'], 422); } } catch (Exception $e) { return response()->json(['error' => $e->getMessage()], 422); } }