compact function returning undefined - laravel

I have a variable which I'm trying to send to the view. If the main image exists then I will send it to the view, if it doesn't exist I will set it ""; I don't know why the code isn't working. Can anyone help to explain it to me?
foreach($post->images as $image){
$images[] = $image->image;
if(!empty($images[0])){
$mainImage = $images[0];
} else {
$mainImage = null;
}
}
return view('pages.post', compact('post', 'tags', 'previous', 'next', 'related', 'latest', 'latestSideCol', 'mainImage'));

Remove the if statement from the foreach statement
foreach($post->images as $image){
$images[] = $image->image;
}
if(!empty($images[0])){
$mainImage = $images[0];
} else {
$mainImage = null;
}

Related

Laravel: How to use multiple where with Request

This code is working. It checks for the exact barcode, case sensitive:
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
However, I need to add one more WHERE clause to check if Expiration is greater than today. I tried this but it does not work.
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->where('expiration','>',new Date())
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
This should work:
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->whereDate('expiration','>', date('Y-m-d'))
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
PS: as far as I understand, your question isn't about multiple "where" conditions in the request, as you've already achieved that in the working example, instead, it's about making a filter for date values.
Check Laravel 7.x Where Clauses docs(still true for version 5.x and 6.x)
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$whereClauses= [[Patron::raw("BINARY `barcode`"), '=', $findpatronbarcode],
['debarred','=', NULL],
['expiration','>', Carbon::now()]];
$patronbarcode = Patron::where($whereClauses)->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}

Codeigniter3.x calling multiple stored procedure cache

This is somewhat related to [CodeIgniter active records' problems calling multiple stored procedures
but I am not experiencing a blank page; instead when I am passing my data array to view it seems that the the preceding array also being drag to the view.
model
public function data1($student) {
$year = 1;
$sem = 1;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql, array($course, $student, $year, $sem));
if (!$query) {
return $this->db->error();
} else {
mysqli_next_result( $this->db->conn_id );
return $query->result();
}
}
public function data2($student) {
$year = 1;
$sem = 2;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
} else {
mysqli_next_result( $this->db->conn_id );
return $query->result();
}
}
Controller:
$data['data1']=data1 from my model(SP);
$data['data2']=data2 from my model(SP);
View:
foreach($data2 as key => $value ) {
echo ....;
}
Here's the PROBLEM... in the view, I only wanted to output the $data2 but to my surprise $data1 is also being output.
Does anybody else have this issue?
I JUST SOLVE IT.
Model
public function data1($student){
**$this->db->initialize();**
$year = 1;$sem = 1;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
}else {
mysqli_next_result( $this->db->conn_id );
return $query->result();**$this->db->close();**
}
}
public function data2($student){
**$this->db->initialize();**
$year = 1;$sem = 2;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
}else {
mysqli_next_result( $this->db->conn_id );
return $query->result();**$this->db->close();**
}
}
controller
$data['data1']=data1 from my model(SP);
**$this->db->close();**
$data['data2']=data2 from my model(SP);

How to check if an item in an array has been removed and delete it?

I have a question that has options like multiple choice options for answering, those options are what is inside the array, if someone removes an option while editing and saves the question, I'm not sure how to check for that, my code checks for editing an existing option and checks for adding a new option.
This is my code for the update
public function update(Request $request, $id)
{
DB::beginTransaction();
$preg = SurveyQuestion::findOrFail($id);
$preg->question = $request->question;
$preg->survey_section_id = $request->survey_section_id;
$preg->response_type_id = $request->response_type_id;
$preg->optional = $request->optional;
$preg->save();
if ($request->get('questionOptions')) {
foreach ($request->get('questionOptions') as $item) {
$opts = [];
if (empty($item['id'])) {
$option = new SurveyQuestionOption();
$option->survey_question_id = $preg->id;
$opts[] = $item['id'];
} else if (!empty($item['id'])) {
$option = SurveyQuestionOption::findOrFail($item['id']);
$opts[] = $item['id'];
}
$option->option = $item['option'];
$option->save();
}
}
DB::commit();
return back();
}
Currently I'm not checking for if its being removed, because like I said I'm not sure how, my idea was creating an array and storing the ids of the ones being created or edited and somehow using that to compare to the array of options being sent to the controller from the vue. Not sure how viable this is, any help is appreciated.
This is the solution I arrived at
public function update(Request $request, $id)
{
DB::beginTransaction();
$preg = SurveyQuestion::findOrFail($id);
$preg->question = $request->question;
$preg->survey_section_id = $request->survey_section_id;
$preg->response_type_id = $request->response_type_id;
$preg->optional = $request->optional;
$preg->save();
$ids = [];
if ($request->get('questionOptions')) {
foreach ($request->get('questionOptions') as $item) {
if (empty($item['id'])) {
$option = new SurveyQuestionOption();
$option->survey_question_id = $preg->id;
} else if (!empty($item['id'])) {
$option = SurveyQuestionOption::findOrFail($item['id']);
}
$option->option = $item['option'];
$option->save();
$ids[] = $option['id'];
}
}
if (count($ids) > 0) {
SurveyQuestionOption::whereNotIn('id', $ids)->where('survey_question_id', $preg->id)->delete();
}
DB::commit();
return back();
}

Property id does not exist on this collection instance

I want to add the Interest_status key on my response. But shows me error. Here is my code. Please Help. Thanks in advance.
$activities_status = [];
$activities = [];
foreach ($activitiesDetail as $activityDetail) {
$activity = '';
if(in_array($activityDetail->user_id, $userFriendIds))
{
$activity = Activity::where('id', $activityDetail->id)->with('joins')->get();
}
else {
$activity = Activity::where('id', $activityDetail->id)
->where('activity_privacy_visible', 0)->with('joins')->get();
}
$interest = $authUser->interests()->where('activity_id', $activity->id)->first();
if($interest)
{
$status['interest_status'] = $interest->status;
$activities_status[] = array_merge($status, $activity->toArray());
} else {
$status['interest_status'] = NULL;
$activities_status[] = array_merge($status, $activity->toArray());
}
$activities = array_merge($activities, $activities_status);
}
return response()->json(['filter'=> $activities], 200);
If I print the Interest_status it gives me the value of status but I return the whole response it shows error.Please Help.

CodeIgniter: I can’t to insert file_name image in database

That's code is in my controller… i can`t to insert name of image in database, except to upload in directory /uploads/
code for insertion file_name of image is below: $data[‘file_name’] = $_POST[‘file_name’];
please help me because needed for quickly.. thank you very much
public function edit ($id = NULL)
{
// Fetch a article or set a new one
if ($id) {
$this->data[‘article’] = $this->article_m->get($id);
count($this->data[‘article’]) || $this->data[‘errors’][] = ‘article could not be found’;
}
else {
$this->data[‘article’] = $this->article_m->get_new();
}
// Set up the form
$rules = $this->article_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->article_m->array_from_post(array(
‘cat_id’,
‘title’,
‘url’,
‘body’,
‘pubdate’
));
/*
* upload
*/
$config[‘upload_path’] = ‘c:/wamp/www/uploads/’;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘max_size’] = ‘1000’;
$config[‘max_width’] = ‘10240’;
$config[‘max_height’] = ‘7680’;
$field_name = “file_name”;
$this->load->library(‘upload’, $config);
if( $this->upload->do_upload($field_name)){
print “uploaded”;
die(“uploaded”);
} else {
$error = array(‘error’ => $this->upload->display_errors());
print_r($error);
die();
}
//end of upload
//insert file_name
$data[‘file_name’] = $_POST[‘file_name’];
$this->article_m->save($data, $id);
}
// Load the view
$this->data[‘subview’] = ‘admin/article/edit’;
$this->load->view(‘admin/_layout_main’, $this->data);
}
You may try something like this
if( $this->upload->do_upload($field_name) )
{
// get upload data
$upload_data = $this->upload->data();
$data[‘file_name’] = $upload_data['file_name'];
$this->article_m->save($data, $id);
//...
}
else
{
//...
}

Resources