Laravel Illegal operator and value combination on where clause - laravel

There are more than 10 controllers in my project. They all have the same code, only the table names are different.
Exactly 2 of these controllers have a problem with the shared host that is 'Illegal operator and value combination'. Surprisingly other controllers works without making such a mistake.
Maybe I overlooked something.
That's where the 'Illegal operator and value combination' problem comes from.
$tarifs = DB::table('tarifs as tarif')
->join('companies as comp', 'tarif.company_id','=', 'comp.id')
->join('categories as cats', 'tarif.catid', '=', 'cats.id')
->where('tarif.company_id', '<', env('COMKEY'))
->get(['tarif.*', 'comp.name', 'comp.photo as cimage','cats.cat_uz']);
Showing this line as an error ->where('tarif.company_id', '<', env('COMKEY'))
This one works without errors.
$minutes = DB::table('minutes as minute')
->join('companies as comp', 'minute.company_id','=', 'comp.id')
->join('categories as cats', 'minute.catid', '=', 'cats.id')
->where('minute.company_id', '<', env('COMKEY'))
->get(['minute.*', 'comp.name', 'cats.cat_uz']);
The project is running normally and without errors on localhost.
Shared and local php versions are the same 7.4

When env('COMKEY') returns the null you will get this error:
InvalidArgumentException with message 'Illegal operator and value combination.'
Code:
$minutes = DB::table('tarifs as tarif')
->join('companies as comp', 'tarif.company_id','=', 'comp.id')
->join('categories as cats', 'tarif.catid', '=', 'cats.id')
->when($comKey,function($query) use ($comKey){
logger()->info('Comkey value is not null',[
'comKey'=> $comKey
]);
return $query->where('tarif.company_id', '<', env('COMKEY'));
},function($query) use ($comKey){
logger()->info('Comkey value is null');
return $query;
})
->dd();
Please use the above query to debug your query.
If the value is returned from env the log will be something like
Comkey value is not null {"comKey":"RandomKey"}
If the value is returned from env the log will be something like:
Comkey value is null

Related

Laravel hasFile() return true even if file is not uploaded

If I uploaded the file works all perfectly, but when I avoid to upload it ( is optional ) I get this error:
fread(): read of 8192 bytes failed with errno=21 Is a directory
In my controller I tried to check if file exists with the function hasFile but it seems doesn't work:
if($request->hasFile('my_file')) {
$fileName = time().'.'.$request->my_file->extension();
$request->my_file->move(public_path('uploads'), $fileName);
$content->my_file= $fileName;
}
Also I tried to print the content of $request->my_file to debug with dd($request->my_file). It's null.
The stranger thing is that also if $request->my_file is null this check getting the error:
if($request->file('my_file') != null)
I found and solved the problem.
HasFile works correctly but the error was in a next step of saving.

Laravel postgresql join error. "Operator does not exist"

My laravel query code
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name', 'sample_point_record.animal_name_id', '=','animal_name.tid::VARCHAR)')
->leftJoin('geographic_unit', 'sample_point_record.geography_unit_id', '=','geographic_unit.tid::VARCHAR');
$query->get();
error message ==>
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer
So I try to add the character it needs
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name', 'sample_point_record.animal_name_id', '=','animal_name.tid::VARCHAR')
->leftJoin('geographic_unit', 'sample_point_record.geography_unit_id', '=','geographic_unit.tid::VARCHAR');
$query->get();
error message :
SQLSTATE[42703]: Undefined column: 7 ERROR: column animal_name.tid::VARCHAR does not exist
I don't think you can use raw Postgres SQL inside the a regular Laravel join function. But, you can (and probably have to) use raw SQL with DB::raw. Try this version:
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name a', function($join) {
$join->on(DB::raw("a.tid::VARCHAR"), DB::raw("="), DB::raw("sample_point_record.animal_name_i")) })
->leftJoin('geographic_unit g', function($join) {
$join->on(DB::raw("g.tid::VARCHAR"), DB::raw("="), DB::raw("sample_point_record.geography_unit_id")) })
$query->get();

CI_session and Error Number: 1054 (Unknown column ...)

I have got problem with ci_session. When I am creating query with Active Record and I use session, it throws me Error Number: 1054.
It is caused by Session.php . I am using database session and when it executes sess_write(), then it throws error.
Can I create new instance of $this->db ?
Error is at line no. 289 in Session.php
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
In this place is set $this->CI->db->order_by(‘something’), from my code, before I call $this->session and this is my problem.
EDIT (13:12):
Unknown column 'contract.created' in 'order clause'
UPDATE `ci_sessions` SET `last_activity` = 1393330303, `user_data` = 'some_data' WHERE `session_id` = 'ea330d7194f902b6b38b88d509766560' ORDER BY `contract`.`created` DESC LIMIT 30
Filename: C:\Apache24\htdocs\Trokadero_v5\system\database\DB_driver.php
EDIT 2:
I am sure, it is happening by Active Record , because sequence of AR tasks is:
My_controller.php
$this->db->order_by('contract.created', 'DESC');
$this->session->set_flashdata('order_by_direction', 'DESC');
And then is executing in Session.php, with my own order_by clause
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
I got it.
$this->session->userdata();
$this->session->set_userdata();
etc.
MUST BE used before/after execute your own CRUD ($this->db->...).
In your controller, you have to reset the db query.
$this->db->order_by('contract.created', 'DESC');
// add line of code to get data. After getting data, reset the db object.
$this->db->_reset_write();
$this->session->set_flashdata('order_by_direction', 'DESC');

Codeigniter showing problem with date function

I want to insert values into db. I have a table as registration_patients. My code is:
$data = array(
'patients_first_name' => $this->input->post('first_name'),
'patients_last_name' => $this->input->post('last_name'),
'patients_email' => $this->input->post('email'),
'patients_password'=> $this->input->post('password'),
'patients_birth_date'=> date('Y-m-d', strtotime($this->input->post('day') . "-" .$this->input->post('month') . "-" . $this->input->post('year'))),
'patients_sex'=> $this->input->post('sex'),
'patients_telephone'=> $this->input->post('telephone'),
'patients_codice_fiscale'=> $this->input->post('codice_fiscale'),
'account_active'=> 0
);
return $this->db->insert('registration_patients', $data);
Now it is inserted values into database bus showing some error and warning like that
A PHP Error was encountered
Severity: Warning
Message: strtotime(): It is not safe to rely on the system's timezone
settings. You are required to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those
methods and you are still getting this warning, you most likely
misspelled the timezone identifier. We selected 'Europe/London' for
'BST/1.0/DST' instead
Filename: models/doctors_model.php
Line Number: 22 and A PHP Error was encountered
Severity: Warning
Message: date(): It is not safe to rely on the system's timezone
settings. You are required to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those
methods and you are still getting this warning, you most likely
misspelled the timezone identifier. We selected 'Europe/London' for
'BST/1.0/DST' instead
Filename: models/doctors_model.php
Line Number: 22
In your root_folder/index.php write date_default_timezone_set('America/New_York'); and see if the problem is solved or not. Here is the link of available timezones.

How to properly use Alias in Codeigniter

Here is my code:
$this->db->select('course_name AS Course Name,course_desc AS Course Description,display_public AS Display Status',FALSE);
$this->db->from('courses');
$this->db->where('tennant_id',$tennant_id);
$this->db->order_by('course_name','ASC');
$query = $this->db->get();
and I got an error:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, course_desc AS Course Description, display_public AS Display Status FROM (' at line 1
and I got an error:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, course_desc AS Course Description, display_public AS Display Status FROM (' at line 1
SELECT course_name AS Course Name,
course_desc AS Course Description,
display_public AS Display Status
FROM (`courses`) WHERE `tennant_id` = 'elicuarto#apploma.com'
ORDER BY `course_name` ASC
Filename: C:\wamp\www\coursebooking\system\database\DB_driver.php
Line Number: 330
Try
$this->db->select('course_name AS `Course Name`, course_desc AS `Course Description`, display_public AS `Display Status`', FALSE);
It's the space in your alias that is messing with you.
UPDATE
I'm not sure why you would want to, but I see nothing preventing you from writing
$this->db->select("course_name AS `{$variable}`", FALSE);
(showing just one field for simplicity)
UPDATE 2
Should be standard string conversion so I don't know why it doesn't work for you.. there's always split strings...
$this->db->select('course_name AS `' . $variable . '`', FALSE);

Resources