Just a quick question guys really thankful for everyone who can help.
How do I foreach loop inside create eloquent in laravel
Here is my code. I'm trying to get and match the answers for their respective survey questions
public function answer_survey(Request $request, $id)
{
$userId = Auth::user()->id;
$user = User::where('id', $userId)->first();
$userNectarPoint = UserNectarPoint::where('user_id', $userId)->first();
$survey = Survey::find($id);
$survey_questions = SurveyQuestion::where('survey_id', $survey->id)->get();
$current_nectarpt_value = $userNectarPoint->nectar_points;
$questionIds = $request->id;
$questions = $request->question;
$answers = $request->answer;
foreach ($answers as $answer) {
}
foreach ($survey_questions as $survey_question) {
$userAnsweredSurvey = new UserAnsweredSurvey;
$userAnsweredSurvey->user_id = $userId;
$userAnsweredSurvey->user_fullname = $user->first_name . ' ' . $user->middle_initial . ' ' . $user->family_name;
$userAnsweredSurvey->survey_id = $survey->id;
$userAnsweredSurvey->survey_title = $survey->title;
$userAnsweredSurvey->survey_question_id = $survey_question->id;
$userAnsweredSurvey->survey_question = $survey_question->question;
$userAnsweredSurvey->answer = $answer;
$userAnsweredSurvey->save();
}
$result_nectarpt = $current_nectarpt_value + $survey->reward_points;
$userNectarPoint->nectar_points = $result_nectarpt;
$userNectarPoint->save();
return back()->with('status', 'survey_done');
}
Related
I am trying to use image url instead of base64. i kindof figured a way how to get images out of the editor and save it inside the directory, but i am trying to insert it into database. i get this error.
Object of class DOMElement could not be converted to string
below i have added the store function inside my controller
public function store(Request $request)
{
//
$data = $request->all();
if (!$request->has('published')) {
$data['published'] = 0;
} else {
$data['published'] = 1;
}
if (!$request->has('featured')) {
$data['featured'] = 0;
} else {
$data['featured'] = 1;
}
$img = null;
if ($request->hasfile('title_img')) {
$image = $request->file('title_img');
$name = time() . '.' . $image->getClientOriginalExtension();
$image->move(public_path() . '/uploads/blogs/', $name);
$img = '/uploads/blogs/' . $name;
}
/* summernote */
$detail=$data['description'];
$dom = new \DomDocument();
$dom->loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$images = $dom->getElementsByTagName('img');
foreach($images as $k => $img){
$data = $img->getAttribute('src');
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$image_name= "/uploads/summernote/" . time().$k.'.jpeg';
$path = public_path() . $image_name;
file_put_contents($path, $data);
$img->removeAttribute('src');
$img->setAttribute('src', $image_name);
}
$detail = $dom->saveHTML();
/* summernote */
$data['title_img'] = $img;
$data['user_id'] = auth()->user()->id;
$title = $data['title'];
$data['slug'] = Str::slug($title);
auth()->user()->posts()->create($data);
return Redirect::route('admin.blog.index')->withSuccess('Whoopie!! New Blog Added!');
}
I hope i can get some help regarding this. Thanks in advance
my question is about how to split this code. I have a registration form and it's saving function looks like this:
public function store(EntityRequestCreate $request)
{
$geoloc = new Geoloc;
$geoloc->lat = $request->input('lat');
$geoloc->lng = $request->input('lng');
$geoloc->slug = $request->input('name');
$geoloc->save();
$user_id = Auth::id();
$entity = new Entity;
$entity->name = $request->input('name');
$entity->type = $request->input('type');
$entity->email = $request->input('email');
$entity->tags = $request->input('tags');
$entity->_geoloc()->associate($geoloc);
$entity->save();
$entity_id = $entity->id;
$address = new Address;
$address->building_name = $request->input('building_name');
$address->address = $request->input('address');
$address->town = $request->input('town');
$address->postcode = $request->input('postcode');
$address->telephone = $request->input('telephone');
$address->entity_id = $entity_id;
$address->save();
$role = User::find($user_id);
$role->role = "2";
$role->save();
DB::table('entity_user')->insert(array('entity_id' => $entity_id, 'user_id' => $user_id));
$result = $geoloc->save();
$result2 = $entity->save();
$result3 = $address->save();
$result4 = $role->save();
if ($result && $result2 && $result3 && $result4) {
$data = $entity_id;
}
else {
$data = 'error';
}
return redirect('profile/entity');
}
As you see, it has a custom request and it is saving to 3 models, that way my controller code is far too long (having many other functions etc) Instead I want to move this code to a model, as my model so far has only relationships defined in it. However I don't exactly know how to call a model from controller, do I have to call it or it will do it automatically? Any other ideas on how to split the code?
You could use the models create method to make this code shorter and more readable.
For example:
$geoloc = Geoloc::create(
$request->only(['lat', 'lng', 'name'])
);
$entity = Entity::create(
$request->only(['name', 'type', 'email', 'tags])
);
$entity->_geoloc()->associate($geoloc);
$address = Address::create([
array_merge(
['entity_id' => $entity->id],
$request->only(['building_address', 'address', 'town'])
)
])
...
The create method will create an object from a given associated array. The only method on the request object will return an associated array with only the fields for the given keys.
how can I check, if current user is in usergroup called for example "Friends" (title) in Joomla 3?
$user = JUser::getInstance($result->id);
$groups = $user->getAuthorisedGroups(); // just IDs, not title
Thanks.
Check this code, I hope it will help you.
$AllGroups = $user->get('groups');
// getting all the groups
foreach($AllGroups as $groupId) {
$user = JFactory::getUser(); // getting user data
$db = JFactory::getDBO();
$userid = $user->get('id'); // logged in user id
$groups = JAccess::getGroupsByUser($userid);
$groupid_list = '(' . implode(',', $groups) . ')';
$query = $db->getQuery(true);
$query->select('id, title');
$query->from('#__usergroups');
$query->where('id IN ' .$groupid_list);
$db->setQuery($query);
$rows = $db->loadRowList();
$grouplist = '';
foreach($rows as $group)
{
if ($groupId == $group[0]) { // checking the logged in user group
$grouplist = $group[1];
}
}
var_dump($grouplist); // it will give you the usergroup name
}
$user = JUser::getInstance($result->id);
$AllGroups = $user->get('groups');
$groupNames = array();
$userid = $user->get('id'); // logged in user id
$groups = JAccess::getGroupsByUser($userid);
$groupid_list = '(' . implode(',', $groups) . ')';
foreach ($AllGroups as $groupId) {
$query = $db->getQuery(true);
$query->select('id, title');
$query->from('#__usergroups');
$query->where('id IN ' . $groupid_list);
$db->setQuery($query);
$rows = $db->loadRowList();
$grouplist = '';
foreach ($rows as $group) {
if ($groupId == $group[0]) { // checking the logged in user group
$grouplist = $group[1];
}
}
array_push($groupNames, $grouplist);
}
$query = $db->getQuery(true)
->select('*')
->from('#__myTable');
if (in_array("Friends", $groupNames)) {
// User is in group "Friends"
}
With some changes I got the error, This work fine in raw php but in codeigniter I'm trying to get this result_array in a while(list), but I don't know what would be the alternative for fetch_row in codeigniter. Please help me to get this in a way at while (list($d, $sn, $s) = each($IDS-)) line.
Model:
public function raw_attendance_Data() {
$this->db->select('attnDate, TraineeID, Status');
$this->db->from('tbl_attendance');
$this->db->order_by('TraineeID');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function rawAttendance(){
$data['IDS'] = $this->AttendanceModel->raw_attendance_Data();
$this->load->view('attendance/rawAttendance', $data);
}
View:
$curname='';
$tdata = '';
reset($IDS);
while (list($d, $sn, $s) = each($IDS)) {
if ($curname != $sn) {
if ($curname) {
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
}
$rowdata = $emptyRow;
$curname = $sn;
}
$rowdata[$d] = $s;
}
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
$query->result_array(); returns an array and not an object. Thus the $IDS in $IDS->fetch_row() is an array (a non-object).
I don't understand, where you are getting the function fetch_row()?
If I understand what you are trying to accomplish then this should work.
View:
$tdata = '';
foreach($IDS as $row)
{
$curname = isset($row['TraineeID']) ? $row['TraineeID'] : "";
$date = isset($row['attnDate']) ? $row['attnDate'] : "";
$status = isset($row['Status']) ? $row['Status'] : "";
if(!empty($curname){
$tdata .= "<tr><td>$curname</td><td>$date</td><td>$status</td></tr>";
}
}
I am trying to use AND as well as && in this code block:
public function show($id)
{
$user = Auth::user()->id;
$userEmail = Auth::user()->email;
$share = Share::where('sbj_id', $id)->first();
if ($share->sbj_id == $id && $share->email == $userEmail ) {
$items = Item::where('project_id', $id)
->orderBy("created_at", "desc")
->groupBy('label_name')
->get();
$sbj = Sbj::find($id);
$allSbj = Sbj::where('user_id', $user)->orderBy("created_at", "desc")->get();
$labels = Label::all();
$pages = Page::where('project_id', $id)->orderBy('created_at', 'desc')->get();
$people = Friend::where('sbj_id', $id)->orderBy("created_at", "desc")->get();
$myPeeps = Contact::where('my_id', $user)->get();
return View::make('sbjs.show', compact('sbj', 'pages', 'labels', 'people', 'myPeeps', 'allSbj', 'items'));
} else {
return Redirect::action ('SbjsController#index');
}
}
However, I am looking online to see if && or AND is supported in Lavavel in this context but no success. My app is taking the user to the ELSE action.
Any ideas?
I just added the Eloquent statement to the IF conditional and it worked fine...not sure why it was not evaluating.