While trying to update database using Excel:
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
$this->gradelevel = $row['grade_level_code'];
$employeegrade = HrEmployee::where('company_id',auth()->user()->company_id)->where('employee_code', $row['staff_id'])->where('email', $row['official_email']) ->first();
$employeegrade->grade_level_id = $this->getGradeLevel();
$employeegrade->save();
}
}
public function getGradeLevel(){
if(!empty($this->gradelevel) || !$this->gradelevel){
return HrGradeLevel::where('grade_level_code',$this->gradelevel)->where('company_id',Auth::user()->company_id)->pluck('id')->first();
} else {
return 0;
}
}
I got this error:
production.ERROR: Creating default object from empty value
and it points to this line:
$employeegrade->grade_level_id = $this->getGradeLevel();
How do I get this resolved?
most probably this will return null
$employeegrade = HrEmployee::where('company_id',auth()->user()->company_id)->where('employee_code', $row['staff_id'])->where('email', $row['official_email']) ->first();
You can also use firstOrNew() if not exist then will create new object of HrEmployee
$employeegrade = HrEmployee::firstOrNew(array("column"=>value, "column2"=>value));
if you want update the existing record only use as
$employeegrade = HrEmployee::where('company_id',auth()->user()->company_id)->where('employee_code', $row['staff_id'])->where('email', $row['official_email']) ->first()
if(!$employeegrade){
//log error employee nit found
}else
{
//update record
}
Related
The code works perfectly when I want to create a new tag from scratch, but when $skillsQuery->count() > 0 and enters in the if statement. It prints...
Method Illuminate\Database\Eloquent\Collection::tag does not exist.
How can I update tags using this package?
Controller
<?php
public function storeSkills(Request $request)
{
$id = auth()->user()->id;
$skillsQuery = Skill::where('created_by', $id)->get();
// If skill exists
if ($skillsQuery->count() > 0) {
$input = $request->all();
$tags = explode(", ", $input['name']);
// $skill = Skill::create($input);
$skillsQuery->tag($tags);
$skillsQuery->created_by = $id;
if ($skillsQuery->save()) {
return redirect()->route('profile')->with('success', 'Skills updated successfully');
} else {
return redirect()->route('profile')->with('error', 'Error updated your Skills!');
}
} else {
$input = $request->all();
$tags = explode(", ", $input['name']);
$skill = Skill::create($input);
$skill->tag($tags);
$skill->created_by = $id;
if ($skill->save())
return redirect()->route('profile')->with('success', 'Skills stored successfully');
else {
return redirect()->route('profile')->with('error', 'Error storing your Skills!');
}
}
}
The result of calling ->get() on a Illuminate\Database\Query is that you will receive an instance of a Illuminate\Database\Collection, which does not contain a ->tag() method. Even if it was a query (by removing ->get()) this still would not work, as you can't call a relationship method off of a collection.
If instead you loop over the skillsQuery then you will receive an instance of a Model object which then allows you to access functions and/or relationships off of it:
$skillsQuery->each(function ($skill) use ($tags) {
$skill->tag($tags); // or perhaps ->retag($tags); here
});
I am fairly new to CodeIgniter and I'm sure there's a simple answer. I need to determine if there are records returned prior to executing this line of code in my VIEW:
foreach ($announcements->result_array() as $announcement_data){
My disconnect is that I can't check if array is empty because it is not an array yet until the foreach line is executed. Checking if $announcements is empty also doesn't work.
CONTROLLER:
public function summary() {
$this->load->model('Propinfo_model');
$data['propid'] = "516";
$data['propinfo'] = $this->Propinfo_model->get_propinfo($data['propid']);
// query for proposal info
$data['announcements'] = $this->Propinfo_model->get_announcements($data['propid']);
$this->load->view('summary_view', $data);
}
MODEL:
// Select all records from 'prop_announcements' table per prop ID
public function get_announcements($propid) {
// Build query
$this->db->select('*');
$this->db->from('prop_announcements P');
$this->db->join('ssp_users', 'P.user = ssp_users.userid');
$this->db->where('P.propid', $propid);
return $this->db->get();
}
VIEW:
foreach ($announcements->result_array() as $announcement_data){
echo $announcement_data['user']; // example field
}
You use
if ($announcements->num_rows() == 0) {
//code for no rows
}
https://www.codeigniter.com/userguide3/database/results.html
I am new to laravel and trying to have a result.
My code is this:
class GegonosController extends Controller
{
public function index($gid = null, $cid = null, $nid = null)
{
if (is_null($cid) and is_null($nid) and is_null($gid)) {
$gegon = Gegono::orderBy('created_at','desc')->get();
}
else{
if(!is_null($gid)) {
if($gid == 0) {
$gegon = Gegono::where('gegtype_id','>',1);
}
else{
$gegon = Gegono::where('gegtype_id', '=', $gid);
}
}
else {
$gegon = Gegono::where('gegtype_id','>',0);
}
if (!is_null($cid)) {
$gegon->where('city_id', '=',$cid);
}
if (!is_null($nid)) {
$gegon->where('nomos_id','=', $nid);
}
$gegon->orderBy('created_at','desc')->get();
}
$nomoi = \App\Nomoi::orderby('name')->get();
return view('front.gegonota', compact('gegon','gid','cid','nid','nomoi'));
}
In the first, if when all variables are null the result is a collection of all the records of the table.
In all other, if (other cases) the result is a builder and I get no results,
It doesn't show any error but gives no results.
Any help would save me a lot of time.
Query Builder's get() method returns the collection of records. In your code, you just call get() method in the air, so it does nothing. You should assign it to a variable or use it in an expression.
Source: https://laravel.com/docs/5.4/eloquent#collections
I an developing a page to create, update, delete and view an event in which there is error while updating the event. There is a event table and a event_collection table. In that event_collection table there is event_id which is id of an event and a collection_id which is from other table collection.
When i create an event, all the data gets stored in event table except the collection one. in the collection table data gets stored in one by one manner like if I check 2 items in collection, it will generate 2 ids with same event_id and 2 collection_ids.
There is problem in update, when i try to update the code, it gives me error as
BadMethodCallException in Macroable.php line 81:
Method update does not exist.
Update method is:
public function update(EventRequest $request, $id)
{
$event = Event::findOrFail($id);
$input = $request->all();
$input['days_of_week'] = serialize(Input::get('days_of_week'));
$query = $event->update($input);
$checkbox_selection = Input::get('agree');
$choosen_checkbox = $id;
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
// return $collection_event;
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
// $collection_id = $id;
foreach($collection_event as $k){
// return $k;
if($k->event_id == $choosen_checkbox){
$data = $request->all();
$data['event_id']= $choosen_checkbox;
$data['collection_id'] = $collection;
$collection_event->update($data);
}
}
}
}
My store method is:
public function store(Request $request)
{
$checkbox = Input::get('days_of_week');
$checkbox_selection = Input::get('agree');
// return $checkbox_collection;
$input = $request->all();
$input['days_of_week'] = serialize($checkbox);
$query = Event::create($input);
$event_id = $query->id;
$pro_id = $query->provider_org_id;
/*For the checkbox selection, if they are multiple store each separately*/
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
$collection_id = $query->id;
if($collection_id){
$data = $request->all();
$data['event_id']= $collection_id;
$data['collection_id'] = $collection;
$create_collection = EventCollection::create($data);
}
}
}
return view('event.pic_upload',compact('event_id','pro_id'));
}
Store method works properly! Can someone please tell any solution? I am badly stucked in this.
I do not think the 'update' method works on collections.
This line will return a collection
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
You do not want a collection, rather a query. As given in the docs:
`App\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);`
Try removing the '->get()' from the statement.
I have a model which fetch the data from database is below
public function counselor() {
$inst_id = $this->session->userdata('user_id');
$submission_key=$this->session->userdata('submission_key');
$query = $this->db->query("SELECT * FROM counselor where USER_ID = $inst_id AND submission_key= $submission_key");
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
}
I have tested the $inst_id and $submission_key by printing it and its set.
$inst_id=2 and $submission_key=2016-8 .BUT though I have one record in database with those two field set its not returning the data. What is the case. I have tried with codeigniter get() and where() method too. Still not giving me the result.
Just write your query using active record function. It will help you in escaping string
$this->db->select('*',FALSE);
$this->db->where('USER_ID',$inst_id);
$this->db->where('submission_key',$submission_key);
$query=$this->db->get('counselor');
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}