I am trying to store data by using id as primary key in Laravel 8, I used following code in controller :
public function store(Request $request)
{
$job = new Job();
$job->employeer_id = Auth()->user('employeer')->id;
$job->category = $request->category_name;
$job->job_context = $request->job_context;
$job->keywords = $request->keywords;
$job->title = $request->title;
$job->vacancy = $request->vacancy;
$job->deadline = $request->deadline;
$job->employment_type = $request->employment_type;
$job->location = $request->location;
$job->gender = $request->gender;
$job->age = $request->age;
$job->responsibilities = $request->responsibilities;
$job->education = $request->education;
$job->requirements = $request->requirements;
$job->additional_requirements = $request->additional_requirements;
$job->salary = $request->salary;
$job->benifits = $request->benifits;
$job->apply_instruction = $request->apply_instruction;
$job->save();
Category::where('category_name', '=' , $request->category_name)->increment('no_jobs', 1);
return redirect('/jobs');
}
The error says that :
Trying to get property 'id' of non-object
in this line :
$job->employeer_id = Auth()->user('employeer')->id;```
Instead of
Auth()->user('employeer')->id;
use
Auth::guard('employeer')->id();
or
auth('employeer')->user()->id;
Check if you get the user first
Auth::guard('employeer')->check();
Then put your code in condition if the user object exist.
So basically you should do this
if(Auth::guard('employeer')->check()){
$job = new Job();
$job->employeer_id = Auth::guard('employeer')->user()->id;
// rest of the code
}else{
// handle as you want
}
Related
getting this error in my local server. but this code is running in online
where its not showing any error. now what can i do? error show empty(count($leadcount)) this condition
if(empty(count($leadcount))){
if(!empty($leadManagements)){
$LeadSend = $this->LeadSend->newEntity();
$inqData['requirement_id'] = $Requirements->id;
$inqData['lead_management_id'] = $leadManagements->id;
$inqData['send_date'] = Time::createFromFormat('Y-m-d', date('Y-m-d'));
$inqData['lead_type'] = 'booking';
$inqData['is_active'] = 1;
$inqData['is_delete'] = 1;
$LeadSend = $this->LeadSend->patchEntity($LeadSend, $inqData);
if($this->LeadSend->save($LeadSend)){
$expression = new QueryExpression('booking_lead_count = booking_lead_count + 1');
$expression2 = new QueryExpression('lead_sent_count = lead_sent_count + 1');
$this->LeadManagements->updateAll([$expression,$expression2], ['id' => $leadManagements->id]);
//lead_type send_date
$this->notificationBuyer($this->request->data);
$this->notificationSeller($this->request->data);
}
}
} else {
/// allready send inquery
}
Surely it should simply be either if(empty($leadcount)) or if(count($leadcount) == 0).
I have an afterSave function in my model, the function executes, but in an endless loop. what could be the reason?
The function inserts the data into my DB, but in multiple rows.
thanks.
my function:
public function afterSave($insert)
{
$modelProject = projects::find()
->where(['status' => 2])
->one();
$idProject = $modelProject->pId;
$candidateforproject = new candidateforproject();
// $candidateforproject->id = 3;
$candidateforproject->idProject = $idProject;
$candidateforproject->idCandidate = $this->prId;
$candidateforproject->idQuestionnaire = $this->id;
$candidateforproject->idStatus = 0;
$candidateforproject->insert();
$answerOne = questionsgrades::findOne($this->answer1);
$grade1 = $answerOne->grade;
$answerTow = questionsgrades::findOne($this->answer2);
$grade2 = $answerTow->grade;
$answerThree = questionsgrades::findOne($this->answer3);
$grade3 = $answerThree->grade;
$answerFour = questionsgrades::findOne($this->answer4);
$grade4 = $answerFour->grade;
$answerFive = questionsgrades::findOne($this->answer5);
$grade5 = $answerFive->grade;
$grade = $grade1 + $grade2 + $grade3 + $grade4 + $grade5;
$this->degree=$grade;
$this->save(['degree']);
}
return parent::afterSave($insert);
}
I can see that you are using $this->save(); in AfterSave action. This will cause calling AfterSave() again. So here is the place with loop.
Possible fix is to use $this->update(); instead of $this->save();
but, honestly, I think you should remaster you logic architecture.
I am trying to delete a certain item from a database depending on conditions. Here is what I have:
while ($row = mysql_fetch_array($result)) {
$now = strtotime("now");
$dateArray = date_parse_from_format("n-j-Y", $row["date"]);
$event_date = strtotime($dateArray['year'].'-'.$dateArray['month'].'-'.$dateArray['day']);
// temp user array
$event = array();
if($event_date > $now) {
//Event is in the future
$pid_check =$row["pid"];
$event["pid"] = $row["pid"];
$event["name"] = $row["name"];
$event["longitude"] = $row["longitude"];
$event["latitude"] = $row["latitude"];
$event["pavement"] = $row["pavement"];
$event["traffic"] = $row["traffic"];
$event["environment"] = $row["environment"];
$event["image_b64"] = $row["image_b64"];
$event["date"] = $row["date"];
$event["time"] = $row["time"];
$event["type"] = $row["type"];
// push single product into final response array
array_push($response["events"], $event);
} else {
$result2 = mysql_query("DELETE FROM events WHERE pid = $pid_check");
}
}
But when I try this it comes up blank, when I comment out result2 it works but doesn't delete(duh). How can I get it to delete? Sorry if this is a simple question, my knowledge of the language is not much.
I think $pid_check is getting set only if the condition in your if is TRUE.
It's not getting set for the else branch.
One option is to relocate the assignment of $pid_check before the if test.
I got two nearly same function.
public function getUserAndCommentAndTelephone($paged=NULL)
{
$select = $this->select()->setIntegrityCheck(false)
->from('user',array('name','user_id'))
->join('comment', 'user.user_id = comment.user_id', array('comment_id','text','date'))
->join('telephone', 'telephone.telephone_id = comment.telephone_id', array('number'))
->order(array('comment.comment_id DESC'));
if (null !== $paged) {
$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
$count = clone $select;
$count->reset(Zend_Db_Select::COLUMNS);
$count->reset(Zend_Db_Select::FROM);
$count->from('comment', new Zend_Db_Expr('COUNT(*) AS `zend_paginator_row_count`'));
$adapter->setRowCount($count);
$paginator = new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(5)
->setCurrentPageNumber((int) $paged);
return $paginator;
}
return $this->fetchAll($select);
and another one with where clause in $select. This is only this one different between.
public function getCommentAndUserByTelephone($number,$paged=null) {
$select = $this->select()->setIntegrityCheck(false)
->from('user',array('name','user_id'))
->join('comment', 'user.user_id = comment.user_id', array('text','comment_id','date'))
->join('telephone', 'telephone.telephone_id = comment.telephone_id', array('number'))
->order(array('comment.comment_id DESC'))
->where('telephone.number = ?', $number);
if (null !== $paged) {
$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
$count = clone $select;
$count->reset(Zend_Db_Select::COLUMNS);
$count->reset(Zend_Db_Select::FROM);
$count->from('comment', new Zend_Db_Expr('COUNT(*) AS `zend_paginator_row_count`'));
$adapter->setRowCount($count);
$paginator = new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(5)
->setCurrentPageNumber((int) $paged);
return $paginator;
}
return $this->fetchAll($select);
}
and on this with where clause a got error :
Column not found: 1054 Unknown column 'telephone.number' in 'where clause'
when I COMMENT it //->where('telephone.number = ?', $number); start working.
so this mine I something wrong with this WHERE clause.
Someone got idea how to change $select, with where clause to make it working.Thank
I think the issue is that you're creating a new select object for the count, but when you reset the FROM clause this removes the joins as well. You'd need to repeat the joins in your count select for what you have to work. However, most of that code is unnecessary, as ZF can build the count query for you based on the select object you pass in.
Try this:
public function getCommentAndUserByTelephone($number,$paged=null) {
$select = $this->select()->setIntegrityCheck(false)
->from('user',array('name','user_id'))
->join('comment', 'user.user_id = comment.user_id', array('text','comment_id','date'))
->join('telephone', 'telephone.telephone_id = comment.telephone_id', array('number'))
->order(array('comment.comment_id DESC'))
->where('telephone.number = ?', $number);
if (null !== $paged) {
$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
$paginator = new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(5)
->setCurrentPageNumber((int) $paged);
return $paginator;
}
return $this->fetchAll($select);
}
That just reuses your existing select instead.
I want to add pagination in my site when user search for some item. I have tried the following code:
//Array Declaration//
$pages = array();
$userlist = array();
//paging variable//
$userlist_pg = $_GET['list_pg'];
if(empty($userlist_pg))
$userlist_pg = 1;
else
$userlist_pg=$_GET['list_pg'];
$userlist_limit = 10;//ADMIN_ITEMLIST_PER_PAGE;
$userlist_start = (($userlist_pg - 1) * $userlist_limit );
$userlist_currentpage = $userlist_pg;
$userlist_back = $userlist_pg-1;
$userlist_next = $userlist_pg + 1;
$query_string = "select cms_id,cms_variable,cms_page_name,cms_last_edited from tbl_cms";
//Paging variables start from here-------------------------
$orderlist = array();
$paging = new PagedResults();
$paging->TotalResults = table_query_count($query_string);
$InfoArray = $paging->InfoArray();
$query_string.=" LIMIT ".$InfoArray["MYSQL_LIMIT1"].", ".$InfoArray["MYSQL_LIMIT2"];
$PageVarName = 'client_page';
$smarty->assign("page_display", getpagelist($InfoArray["CURRENT_PAGE"],$InfoArray["PREV_PAGE"],$InfoArray["NEXT_PAGE"],$InfoArray["TOTAL_PAGES"],$InfoArray["Second_next"],$InfoArray["third_next"],$InfoArray["fourth_next"],$PageVarName));
$smarty->assign("currentpage",$InfoArray["CURRENT_PAGE"]);
$smarty->assign("total_pages",$InfoArray["TOTAL_PAGES"]);
//Paging variables end here-------------------------
it gives the following errors.
Call to undefined function table_query_count() in /home/www/jobplacement4u.com/hungry_uni/modules/search/action/search_action.php on line 110
Class 'PagedResults' not found in /home/www/jobplacement4u.com/hungry_uni/modules/search/action/search_action.php on line 30
Call to undefined method PagedResults::InfoArray() in /home/www/jobplacement4u.com/hungry_uni/modules/search/action/search_action.php on line 111
Looks like you forgot to include the file(s) with definition of function table_query_count and class PagedResults