date callback function in ignited datatables not working - codeigniter

im using ignited datatables library for codeigniter
this is my code:
function datatable()
{
$this->datatables->select('id,name,created')
->unset_column('id')
->edit_column('created', '$1',date('Y-m-d H:i:s','created'))
->from('categories');
echo $this->datatables->generate();
}
created is unix timestamp and i want to show date in table.
but when i use callback function like this or in helper it doesn`t work. php warning:
A PHP Error was encountered
Severity: Warning
Message: date() expects parameter 2 to be long, string given
Filename: controllers/categories.php
Line Number: 22
created is unix timestamp value in database. when i pass it to date function the database value does not pass to date function. when i var_dump it it shows $1 instead of real value of database. in other functions it works ok. but in date function returns error.

Use this
$this->datatables->select('id,name,created')
->unset_column('id')
->edit_column('created', '$1',timestamp('Y-m-d H:i:s'))
->from('categories');
Timestamp Manual

Related

Store Helpers functions in mysql Table Undefined variable $employee_id

I'm working on Laravel and I have functions in the helper. I want to store the functions in the data base (mirgation).
Ex function in the helper :
function PayrollSalary($employee_id, $employor_id)
I can't store the below line when I migrate the database. I have an error message (Undefined variable $employee_id).
DB::table('payroll_model_items')->insert(array(
'code'=>"BaseSal",
'name'=>"Base Salary",
'value'=>"<div>{{PayrollSalary($employee_id, $employor_id)}}</div>",
'sequence'=>"0000101",
'created_at'=>date('Y-m-d H:m:s'),
'updated_at'=>date('Y-m-d H:m:s')
));
Can anyone help?
Thank you

Laravel 8: why is sorting by date in eloquent is wrong, using Carbon

Possible duplicates:
Laravel: How to order date, change the date format and group by the date?
Laravel Carbon format wrong date
I created a line chart using chartjs, the chart and data fetching is working fine. However the order of the dates is wrong. It starts off with Aug 2022 instead of Jan 2022.
When I use the orderBy(), it shows orderBy() doesn't exists error.
When I use createFromFormat() of Carbon, it shows missing data error.
Why? I parsed the date with Carbon and the column type is datetime, shouldn't it be working?
This is my laravel collection:
$data = TrafficViolation::select('id', 'violationDateTime')
->get()
->sortBy('violationDateTime')
->groupBy(function($data) {
return Carbon::parse($data['violationDateTime'])->format('M, Y');
});
The orderBy() is a Query Builder method. Once you call get(), you get a Collection instance.
To order the records in your DB you need to call orderBy() first, and than get():
UPDATE:
I have included the records count, and date format. You still need to order the records by the violationDateTime column
$data = User::selectRaw('COUNT(*) as violation_count, DATE_FORMAT(violationDateTime, \'%M, %Y\') as formatedDate')
->groupBy('formatedDate')
->orderBy('violationDateTime')
->get();
if your get a Syntax error or access violation: 1055 you need to change the stritc mode in mysql to false in /config/database.php change ['connections' => ['mysql' => ['strict' => false]]]

Error when retrieving and updating data from database in Codeigniter

In my application controller, I have a method transaction_reimburse() that receives ids as POST string values. The method's function is to get all transactions that match the ids from the database, and update them accordingly.
Controller method:
public function transaction_reimburse() {
$reimburse = array('reimburse' => 1);
$transactions = $this->Transaction_model->get_these_ids($this->input->post('ids'));
$this->Transaction_model->reimburse_these($transactions, $reimburse);
}
Model method:
function get_these_ids($ids) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $ids .")");
return $this->db->get();
}
Model method:
function reimburse_these($transactions, $reimburse) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $transactions .")");
$this->db->update($this->tbl_transaction, $reimburse);
}
However, I am getting the following error:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: models/transaction_model.php
Line Number: 450
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 ')' at line 1
UPDATE `tbl_transactions` SET `reimburse` = 1 WHERE `tbl_transactions`.`id` IN ()
Filename: C:\xampp\htdocs\ipa\system\database\DB_driver.php
Line Number: 330
What I need to know is why do I get these errors?
The errors mean that your $ids variable is not in a string format (meaning: 1,2,3,4. Its probably an array). To fix it, first find out what format it is on by using print_r($ids) or var_dump($ids). Once you know the format, use PHP functions(such as implode()) to transform it into strings.
IN is looking for a set in the format e.g. (1,2,3,4).
Either make your post values adopt the format 1,2,3,4 so that when the value is included into the query it's valid or if you're sending an array use a method in PHP such as implode(',',$array) to create the desired format for binding into the query..
I have reviewed your both function and found that in both function you put tbl_transactions.id in where clause and ids is passed from post and hence there is no need to call get_these_ids to get ids, you already have ids in post array and hence instead of transactions variable, just use $this->input->post('ids') for reimburse_these functions.
if you surely want to get tbl_transactions from get_these_ids function then you need to use group_concat with group by in query of get_these_ids function and just return that concated id in get_these_ids function then it will work.

Laravel - convert input date format before save

I'm trying to convert date input to the proper mysql format.
My start_date field is formatted MM dd, yyyy.
In my controller store function I'm grabbing all the users input including start_date and saving.
$userProfileData->save();
What I thought I could do is us Carbon to convert the start_date before save like this.
$userProfileData->start_date->toDateString();
I get a Call to a member function toDateString() on a non-object error.
I was able to convert the date format by adding this to my users controller store function.
$userProfileData->start_date = date('Y-m-d', strtotime($userProfileData->start_date));
you get non-object error because start_date is not the object with method toDateString()
what you can do is use setStartDateAttribute() method in your model which will be invoked every time to get the start_date attribute value and in this method you can modify value so model will use the updated value for operation like edit or update it can be as follow
public function setStartDateAttribute($value) {
$this->attributes['start_date'] = date('Y-m-d', strtotime($value) );
}
the following method will used by your model when it needs to get the value of start_date attribute this method must be written in the model

Carbon - A textual month could not be found Trailing data

I'm using Laravel. I get the following error when trying to display a date:
A textual month could not be found Trailing data
Format is j M Y, date is 2014-12-13 10:00:00
I don't get where is the error.
Any toughts?
For future reference: Using createFromFormat with the second parameter not being a Carbon object was giving me this same error. Using Carbon::parse:: instead of createFromFormat seemed to fix the issue:
public function setPublishedAtAttribute($date) {
$this->attributes['published_at'] = Carbon::parse($date);
}

Resources