Getting Undefined offset: 1 in laravel - php

I am new to Laravel and was developing small application for my practise. I am doing job search functionality. This error giving me alot trouble and confuses me alot.
public function job_search(Request $request) {
$search_skill_set = $request->job_skills;
$search_results = JobPost::whereRaw('FIND_IN_SET(?, job_skills)', $search_skill_set)
->get()
->toArray();
for ($i = 0; $i < count($search_results); $i++) {
$department_id = (int)$search_results[$i]['department_name'];
$department_name = Department::select('department_name')
->where('id', '=', $department_id)
->get()
->toArray();
// the next statement raises an Undefined:offset 1 error
$search_results[$i]['department_name_info'] = $department_name[$i]['department_name'];
}
var_dump($search_results);
}
I am not getting where am i doing wrong, so any suggestion from given snippet and any modification in the code

change this line:
$search_results[$i]['department_name_info'] = $department_name[$i]['department_name'];
to
$search_results[$i]['department_name_info'] = $department_name[0]['department_name'];

for ($i=0; $i < count($search_results) ; $i++) {
$department_id = (int)$search_results[$i]['department_name'];
//I am getting department id correct here
$department_name = Department::select('department_name')->where('id','=',$department_id)->get()->toArray();
//$depratment_name is also going okay and working
$search_results[$i]['department_name_info'] = $department_name[0]['department_name'];
// This line should have a static index.
}

Related

Laravel how to ignore empty parameter numbers

I am making a laravel application, Here i can 'filter' out my 'files' first by role (assigned to the user), Folder and then subfolders. for the query i use a for loop, but this can return different values depending on the 'filters' i use.
Now laravel returns this error message:
SQLSTATE[HY093]: Invalid parameter number
Query:
select * from `file` where `id` in (8, 12, 13, ?, ?, ?)
Now i want to ignore those '?' and keep it from returning an error, i know this is doable by making the '?' NULL. but i cant find a way to do that
the code:
public function show($id)
{
$user = Auth::user();
$userid = $user->id;
$role_id = $user->role_id;
$folders = Folder::all();
$file_role = File_Role::where('role_id', '=', $role_id)->pluck('file_id');
// dd($file_role[1]);
$count = count($file_role);
$i = 0;
var_dump($file_role);
for($i = 0; $i < $count; $i++) {
$file[] = File_Subfolder::where('subfolder_id', '=', $id)->where('file_id', '=', $file_role[$i])->pluck('file_id');
}
$chosenfile = File::whereIn('id', $file)->get();
return view('partner.subquents.sub_file', compact('chosenfile'));
}
$file returns 8,12,13,?,?,? in this case. this can change depending on the filters
i tried an foreach loop.and i tried to find on the internet how to assign NULL to '?' values
as you can see in my DD that some dont give data back, but some do
Can u try this:
$fileSubFolderCount = File_Subfolder::where('subfolder_id', '=', $id)->where('file_id', '=', $file_role[$i])->pluck('file_id')->count();
$file[] = $fileSubFolderCount > 0 ? File_Subfolder::where('subfolder_id', '=', $id)->where('file_id', '=', $file_role[$i])->pluck('file_id') : null;

MariabDB synchronous UPDATE query

I'm working with PHP and MariaDB and I run into a problem.
I update a value to multiple rows, and then SELECT there rows to make a new calculation the data for another task.
The problem here that I get the wrong number. I guess that the MariaDB has not finished the UPDATE query, but it return the finished flag to PHP and then the PHP proceeds the SELECT query. [I just guess]
I open to any idea. If I'm wrong, please correct me.
Thank you for sharing
This is my code
$modelAdminOrderBidSys = $this->load->model('Admin\Order\BidSys');
$acceptedItem = typeCast($modelAdminOrderBidSys->getItem($cartItemId));
if (!$acceptedItem) {
return array(
'result' => 'error',
'message' => 'Cannot find item #' . $cartItemId
);
}
$acceptedItem['lastOffer'] = $acceptedItem['offer'];
$acceptedItem['accepted'] = 1;
$acceptedItem['isBot'] = 0;
$modelAdminOrderBidSys->updateItem($cartItemId, array2object($acceptedItem));
$cartItems = typeCast($modelAdminOrderBidSys->getItems($acceptedItem['cartId']));
$accepted = 1;
$total = 0;
$offer = 0;
$lastOffer = 0;
foreach($cartItems as $cartItem) {
if ((int)$cartItem['accepted'] < 1) {
$accepted = 0;
}
$total += (float)$cartItem['total'];
$offer += (float)$cartItem['offer'];
$lastOffer += (float)$cartItem['lastOffer'];
}
$postField = new \stdClass();
$postField->accepted = $accepted;
$postField->total = $total;
$postField->offer = $offer;
$postField->lastOffer = $lastOffer;
$modelAdminOrderBidSys->updateCart($acceptedItem['cartId'], $postField);
It sounds like your SELECT transaction starts before the UPDATE has committed. Try changing the transaction_isolation (in config) / tx_isolation (at runtime with SET GLOBAL) to READ-COMMITTED. Default is REPEATABLE-READ.

Laravel Getting an error Undefined offset: 3

I am getting an error in my laravel application. The error states that Undefined offset:3.
Controller
public function updateVisaDocuments($type, $details, $visa, $documents_id){
$visas_documents_db = \App\VisaDocuments::where ( 'visa_id', $visa->id )->get();
foreach($visas_documents_db as $document_db){
$count=0;
for($j = 0; $j < sizeof ( $documents_id ); $j ++) {
if($document_db->id==$documents_id [$j]){
$count++;
}
}
if($count==0)
$document_db->delete();
}
for($i = 0; $i < sizeof ( $type ); $i++) {
if(!empty($type[$i])){
if(!empty($documents_id[$i])){
$visa_documents = \App\VisaDocuments::where ( 'id', $documents_id[$i] )->first ();
}else{
$visa_documents=new VisaDocuments();
$visa_documents->id=Uuid::generate ();
}
$visa_documents->type=$type[$i];
$visa_documents->details=json_encode($details);
$visa_documents->visa_id=$visa->id;
if(!empty($documents_id[$i]))
$visa_documents->update();
else
$visa_documents->save();
}
}
}
foreach ($visa->documents as $documents){
$i++;
$det=json_decode($documents->details,true);
$size_det=sizeof($det);
$size_det_inside=sizeof($det[$i]);
$k=0;
View
<?php $size_det_inside = sizeof($det[$i]);?>
#for($j=0; $j< $size_det_inside; $j++)
#if($det[$i][$j] != '')
<input type="text" name="document_details[{{$i}}][]" value="{{$det[$i][$j]}}" />
#endif
#endfor
I am getting error here sizeof($det[$i]); (View). I guess, the result is in json format and in my view file i am treating it as variable.
Can anyone please help me on this.
If your php version is 7.0 or above, you should use count() function instead of sizeof(). Please use count().
http://php.net/manual/en/function.sizeof.php

Yii2 ActiveRecord Setting unknown property: app\models\

This code throw Exception:
public function actionSetdubl() {
$dubls = Yii::$app->request->post('dubl');
$parent = Yii::$app->request->post('parent');
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->all()[0];
for ($i = 0; $i < sizeof($dubls); ++$i) {
$val = $dubls[$i];
$jur = JurForm::findOne($val);
$jur->CFLDUBL = 'Yes';
$jur->DUBLMDM_ID = $parentInfo['MDM_ID'];
$jur->DCHANGEDATE = date('Y-m-d H:i:s');
$jur->save();
}
return Yii::$app->getResponse()->redirect('/index.php?r=jur/analysis');
}
on the line with code $jur = JurForm::findOne($val);.
Exception:
Setting unknown property: app\models\JurForm::PKJUR.
DB: Oracle.
ActiveRecord2 has a hard time automatically mapping table names that start with a capital letter.
So for these columns you have to go into your model class and formally declare them:
public $PKJUR;
maybe better?
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->one()
also i think PKJUR is not defined in DB.

pagination with CodeIgniter

Hi i'm trying to get the pagination right with code igniter but it seems that it doesn't want to work correctly. I get the second page, but there the pagination disappears, I still got the right table though and I have 2 errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: models/evaluation_model.php
Line Number: 37
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/evaluation_model.php
Line Number: 37
The controller function:
function showEvaluations($offset = 0)
{
if($this->login->is_logged_in())
{
$limit = 5;
$result = $this->evaluation_model->getAllEvaluations($limit, $offset);
if ($this->session->userdata('type') == 'admin')
{
$data['evaluations'] = $result['evaluations'];
$data['total'] = $result['num_rows'];
$data['notallowed'] = false;
$config = array();
$config['base_url'] = base_url("evaluation/showEvaluations/");
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('allevaluations_view', $data);
}
else
{
$data['notallowed'] = true;
$this->load->view('allevaluations_view', $data);
}
//$this->load->view('allevaluations_view', $data);
}
else
{
$this->load->view('login_view');
}
}
and the Model:
function getAllEvaluations($limit, $offset)
{
$q = $this->db->select('tblPunten.PK_PuntID, tblPunten.Titel, tblPunten.Score, tblVakken.Vak, tblUsers.username, tblUsers.Voornaam, tblUsers.Achternaam')
->from('tblPunten')
->join('tblVakken', 'tblPunten.FK_VakID = tblVakken.PK_VakID')
->join('tblUsers', 'tblPunten.FK_UserID = tblUsers.PK_UserID')
->limit($limit, $offset);
$query['evaluations'] = $q->get()->result();
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
->limit($limit, $offset);
$tmp = $q->get()->result();
$query['num_rows'] = $tmp[0]->count;
return $query;
}
Line 37: $query['num_rows'] = $tmp[0]->count;
Found it, the second query:
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
->limit($limit, $offset);
must be without the limit:
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
In codeigniter, ->result() an object.
if you want in that form of array then we can use ->result_array() so that we can get the result set in the form of array.
$tmp = $q->get()->result_array();//made changes
$query['num_rows'] = $tmp[0]['count'];//will work

Categories